2016-11-15 53 views
0

我試圖做一個循環來詢問用戶選擇它是一個字符串,將其轉換爲整數,並計算出總的和不斷地問,直到用戶輸入q表示放棄。除了我的if語句完全被Python忽略之外,一切都進展順利。誰能幫我?Python是無視我,如果在循環內聲明

到目前爲止我的代碼看起來是這樣的:

l = 0.89 
g = 2.50 
p = 0.50 
t = 0.75 
o = 0.50 
Subtotal = 0 
Total = 0 
q = "" 
quanity = 0 
choice = "" 

choice = input('L - Lettuce \nG - Green Beans \nP - Peppers \nT - Tomatoes \nO - Onions \nS - Seasonal Item \nQ - Quit') 

while choice != q: 
    Subtotal = 0 
    quanity = 0 
    quanity = input('How Many?') 
    if choice == l: 
     choice = 0.89 
    elif choice == g: 
     choice = 2.50 
    elif choice == p: 
     choice = 0.50 
    elif choice == t: 
     choice = 0.75 
    elif choice == o: 
     choice = 0.50 
    Subtotal = choice * quanity 
    Total = Total + Subtotal 
    print('Your total so far is $' ,Total) 
    choice = "" 
    choice = input('L - Lettuce \nG - Green Beans \nP - Peppers \nT - Tomatoes \nO - Onions \nS - Seasonal Item \nQ - Quit') 
print('All Done') 
+0

你所說的「完全忽略了」是什麼意思?究竟發生了什麼?你有沒有諮詢過你[橡皮鴨](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)? –

+0

順便說一句,我幾乎不知道任何蟒蛇,仍然可以使用這個程序沒有問題,只是通過閱讀你的代碼。這強烈地表明你應該閱讀我上面鏈接的文章,並認真對待它。 –

+0

您應該提供少量的測試用例以及實際的輸出和所需的測試用例。否則,很難獲得幫助。看看[這裏](http://stackoverflow.com/help/mcve) –

回答

3

您正在測試從input一個str對包含float值的變量。當然沒有命中。讓測試檢查類似:

choice = choice.upper() 
if choice == 'L': 
    choice = l 
elif choice == 'G': 
    choice = g 

你要知道,一個更簡潔的解決辦法是定義從字母值的映射,這樣你就不會使用,很多鏈接的比較:

# Defined up front to avoid reconstructing over and over 
choicemap = {'L': l, 'G': g, 'P': p, 'T': t, 'O': o} 

# Avoid duplicating input code, infinite loop with break on 'Q' input 
while True: 
    choice = input('L - Lettuce \nG - Green Beans \nP - Peppers \nT - Tomatoes \nO - Onions \nS - Seasonal Item \nQ - Quit').upper() 

    try: 
     choice = choicemap[choice] 
    except KeyError: 
     if choice == 'Q': 
      break 
     print("{!r} is not a recognized option".format(choice) 
     continue 

    try: 
     quanity = int(input('How Many?')) 
    except ValueError: 
     print("Non-numeric input provided, try again") 
     continue 

    Total += choice * quanity 
    print('Your total so far is $', Total) 

print('All Done') 
+0

也可以使用字典'{「L」:0.89,...}' –

+0

@tobias_k:我寫的是另一種解決方案,當您發佈。 :-) – ShadowRanger

+0

此外,while循環檢查輸入是否是'q',他們delcare'Q =「」',所以它不是檢查'「Q」',它檢查'「」'也許他們應該做變量如何工作 –

0
l = 0.89 
g = 2.50 
p = 0.50 
t = 0.75 
o = 0.50 
Subtotal = 0 
Total = 0 
q = "" 
quanity = 0 
choice = "" 

choice = input('L - Lettuce \nG - Green Beans \nP - Peppers \nT - Tomatoes \nO - Onions \nS - Seasonal Item \nQ - Quit') 

while choice != 'q': 
    Subtotal = 0 
    quanity = 0 
    quanity = float(input('How Many?')) 
    if choice == 'l': 
     choice = 0.89 
    elif choice == 'g': 
     choice = 2.50 
    elif choice == 'p': 
     choice = 0.50 
    elif choice == 't': 
     choice = 0.75 
    elif choice == 'o': 
     choice = 0.50 
    Subtotal = choice * quanity 
    Total = Total + Subtotal 
    print('Your total so far is $' ,Total) 
    choice = "" 
    choice = input('L - Lettuce \nG - Green Beans \nP - Peppers \nT - Tomatoes \nO - Onions \nS - Seasonal Item \nQ - Quit') 
print('All Done') 

您需要輸入前增加float,因爲input()返回蟒蛇3.x中的字符串 你還在比較字符串,if choice == 'string':

+0

在這種情況下,多一點研究,在頂部聲明的變量無可奈何.... –

+1

我不知道爲什麼他宣稱他們並沒有使用它們。我只是複製了代碼並修改了錯誤。如果他願意,他會稍後改變它們。 –