我試圖做一個循環來詢問用戶選擇它是一個字符串,將其轉換爲整數,並計算出總的和不斷地問,直到用戶輸入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')
你所說的「完全忽略了」是什麼意思?究竟發生了什麼?你有沒有諮詢過你[橡皮鴨](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)? –
順便說一句,我幾乎不知道任何蟒蛇,仍然可以使用這個程序沒有問題,只是通過閱讀你的代碼。這強烈地表明你應該閱讀我上面鏈接的文章,並認真對待它。 –
您應該提供少量的測試用例以及實際的輸出和所需的測試用例。否則,很難獲得幫助。看看[這裏](http://stackoverflow.com/help/mcve) –