循环时无法逃脱

时间:2016-02-13 20:26:11

标签: python python-3.x

我是一名初学程序员,正在使用Python 3.5编写计算机概念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.")

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

4 个答案:

答案 0 :(得分:3)

似乎你在" lower"的末尾缺少功能 - paranthesis,如下所示:

if cont.lower() not in yorn:

答案 1 :(得分:2)

你的问题在这里:

if cont.lower not in yorn:

lower是一种方法,它应该是:

if cont.lower() not in yorn:

答案 2 :(得分:2)

在您的代码中,您正在执行cont.lower not in yorn[some string].lower是一个函数,而不是属性,所以你必须通过在它后面添加括号来调用它。

cont = input("Would you like to order another? (y/n) ")
if cont.lower() not in yorn:
    valid_data = False
else:
    valid_data = True

答案 3 :(得分:1)

你在.lower之后错过了一个括号:

if cont.lower() not in yorn: