2016-02-13 89 views
3

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

是否有與此代碼的其他問題我也不會感到驚訝但他們很可能按照任務的要求進行設計。歡迎任何額外的反饋。

+0

你寫了,可人們在世界各地被視爲一個問題。沒有必要指定這是什麼班級或學校。 – skrrgwasme

+0

不知道爲什麼這很重要。似乎沒有太多的個人識別。無論如何改變它,因爲你是對的,這是沒有必要的。 – WileyJ

+1

這不是關於個人識別信息,而是沒有人關心。這只是噪音,不會爲您的問題添加任何信息。 – skrrgwasme

回答

3

看來你缺少功能paranthesis中的「較低」結尾,像這樣:

if cont.lower() not in yorn: 
2

你的問題是在這裏:

if cont.lower not in yorn: 

下是一種方法,它應該是:

if cont.lower() not in yorn: 
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 
+0

啊,是的,我現在看到它。謝謝。 – WileyJ

+1

@Wiley J如果可以的話,更喜歡'for'循環到'while'循環。在幾年前修復了一個在while循環中的bug後,我幾乎總是使用for循環。 –

+0

因爲如果'cont.lower not in yorn'不會導致程序失敗,它必須是有效的Python。它到底在做什麼? – Disnami