2016-11-21 71 views
0

我有我的計劃一噸的麻煩。過去5個小時我到處尋找,似乎無法找到任何與我正在嘗試做的事情有關的事情(無論如何我都能理解)。轉換用戶字輸入價格

我創建一個比薩餅程序,並試圖獲得大小的輸入(如大,中,小)。該程序將要求用戶輸入大,中,小,價格爲空白列表。

我已經設置了價格,但我完全搞不清我需要怎麼大,中,小的輸入轉換成實際的價格,我爲大小設置。

這是我的代碼。任何幫助是極大的讚賞。我也半知道我不應該使用int來定價,但是我查看的所有命令都是針對Python的老版本或其他東西。

目前使用Python 3.5。

print ("Welcome to My Pizzeria!") 
name = input("What name is the order under? ") 
size = input("What size would you like? ") 
top = input("Would you like to add sausage? ") 
money = 0 
total = (size+top) 
if size == 'large': 
    total = int(money) + 10.00 
elif size == 'medium': 
    total = int(money) + 7.00 
elif size == 'small': 
    total = int(money) + 5.00 
money = total 
if top == 'no': 
    total = int(money) + 0.00 
elif top == 'yes': 
    total = int(money) + 1.00 
    pass 
print ("Your total is $" + str(total)) 
print ("Thank you for your order " + name + "!") 
+0

什麼是'money',是基礎價格是多少?因爲你的情況它被設置爲'None',這是不正確的。國際海事組織它應該是一些固定的整數值或者你可能想從用戶採取這個輸入 –

+0

錢只是我試圖測試一些想法。我已經更新了我目前正在玩弄的代碼。如果需要更多比薩餅,我必須稍後讓我的程序循環。基本上所有我問的是我怎麼能做一個僞「if」聲明。喜歡:如果回答「你想要什麼尺寸」很大,那麼總共添加10.00 – Zeus

回答

0

在這種情況下,我認爲這是你想要的。 我已經裝滿錢的10僞價值,雖然我仍然包裹在INT的情況下,你擁有了它在海峽格式。

print ("Welcome to My Pizzeria!") 
name = input("What name is the order under? ") 
size = input("What size would you like? ").lower() 
top = input("Would you like to add sausage? ").lower() 
money = 0 
if size == 'large': 
    total = int(money) + 10.00 
elif size == 'medium': 
    total = int(money) + 7.00 
elif size == 'small': 
    total = int(money) + 5.00 
money = total 
if top == 'no': 
    total = int(money) + 0.00 
elif top == 'yes': 
    total = int(money) + 1.00 
print ("Your total is $" + str(total)) 
print ("Thank you for your order " + name + "!") 

OP

Welcome to My Pizzeria! 
What name is the order under? Saurabh 
What size would you like? Large 
Would you like to add sausage? Yes 
Your total is $11.0 
Thank you for your order Saurabh! 
+0

非常感謝!我真的很感謝你的幫助。我在你的代碼中添加了幾行代碼,我即將嘗試添加循環語句。用新代碼更新原始帖子。 – Zeus

0

該代碼會要求用戶回答,直到他們提供一個有效的答案,然後打破無限循環,進入到下一個。您不需要變量「money」,因爲可以在整個腳本中添加總數而不需要額外的變量。 'x + = 1'只是'x = x + 1'的簡稱。希望這可以幫助!

print ("Welcome to My Pizzeria!") 
name = input("What name is the order under? ") 
total = 0 

while 1: 
    size = input("What size would you like? ").lower() 
    if size == 'large': 
     total += 10.00 
     break 
    elif size == 'medium': 
     total += 7.00 
     break 
    elif size == 'small': 
     total += 5.00 
     break 
    else: 
     print("Sorry, that is not an option! Please order something else!") 

while 1: 
    top = input("Would you like to add sausage? ").lower 
    if top == 'no': 
     total += 0.00 
     break 
    elif top == 'yes': 
     total += 1.00 
     break 
    else: 
     print("Sorry, that is not an option! Please order something else!") 

print ("Your total is $" + str(total)) 
print ("Thank you for your order " + name + "!")