gpt4 book ai didi

python - 无法逃脱 while 循环

转载 作者:太空宇宙 更新时间:2023-11-03 12:57:27 25 4
gpt4 key购买 nike

我是一名初级程序员,使用 Python 3.5 编写我的计算机概念 III 类(class)。本周我们正在使用 try/except block 和 bool 标志进行数据验证,修改我们上周编写的程序。除了一件事,我几乎完成了每周的作业。我不明白为什么我会陷入 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.")

如果此代码存在其他问题,我不会感到惊讶,但它们很可能是根据作业要求设计的。欢迎任何其他反馈。

最佳答案

您似乎在“lower”的末尾缺少函数括号,如下所示:

if cont.lower() not in yorn:

关于python - 无法逃脱 while 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35384928/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com