我是一位在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