我是一位在Python 3.5中为我的Computer Concepts III类编写的开始程序员。本周我们正在尝试使用try/except块和布尔标志来修改我们上周制作的程序。除了一件事我几乎完成了我的每周任务。我无法弄清楚为什么我在一个while循环中卡住了。这是有问题的循环:循环时无法逃脱
while not valid_data:
cont = input("Would you like to order another? (y/n) ")
if cont.lower not in yorn:
valid_data = False
else:
valid_data = True
yorn
是["y", "n"]
这里是上下文整个程序:
# Program Lesson 6 Order
# Programmer Wiley J
# Date 2016.02.13
# Purpose The purpose of this program is to take an order for cookies.
# Import Class/Format Currency
import locale
locale.setlocale(locale.LC_ALL, '')
# Define Variables
boxes = 0
cost = 3.50
qty = 0
items = 0
yorn = ["y", "n"]
# Banner
print("Welcome to The Cookie Portal")
# Input
valid_data = False
while not valid_data:
name = input("Please enter your name: ")
if len(name) > 20:
print()
print("Not a valid name")
valid_data = False
elif len(name) == 0:
print("You need to enter a name")
valid_data = False
else:
print("Hello", name)
valid_data = True
cont = input("Would you like to place an order? (y/n) ")
# Process
while cont.lower() not in yorn:
cont = input("Would you like to place an order? (y/n) ")
while cont.lower() == "y":
valid_data = False
while not valid_data:
print("Please choose a flavor:")
print("1. Savannahs")
print("2. Thin Mints")
print("3. Tagalongs")
try:
flavor = int(input("> "))
if flavor in range (1, 4):
items += 1
valid_data = True
else:
valid_data = False
except Exception as detail:
print("Error", detail)
valid_data = False
while not valid_data:
try:
boxes = int(input("How many boxes? (1-10) "))
if boxes not in range (1, 11):
print("Please choose a number between 1 and 10")
valid_data = False
else:
qty += boxes
valid_data = True
except Exception as detail:
print("Error", detail)
print()
print("Please enter a number")
valid_data = False
while not valid_data:
cont = input("Would you like to order another? (y/n) ")
if cont.lower not in yorn:
valid_data = False
else:
valid_data = True
# Output
if cont.lower() == "n":
cost *= qty
print()
print("Order for", name)
print("-------------------")
print("Total Items = {}".format(items))
print("Total Boxes = {}".format(qty))
print("Total Cost = {}".format(locale.currency(cost)))
print()
print("Thank you for your order.")
是否有与此代码的其他问题我也不会感到惊讶但他们很可能按照任务的要求进行设计。欢迎任何额外的反馈。
你写了,可人们在世界各地被视为一个问题。没有必要指定这是什么班级或学校。 – skrrgwasme
不知道为什么这很重要。似乎没有太多的个人识别。无论如何改变它,因为你是对的,这是没有必要的。 – WileyJ
这不是关于个人识别信息,而是没有人关心。这只是噪音,不会为您的问题添加任何信息。 – skrrgwasme