2016-05-28 57 views
1

我在編寫一個程序,它將計算出提示用戶輸入訂單的值和重量並顯示運輸成本的程序。Python if,elif,else if when is not true的問題skip

在線訂單的運費根據訂單價值和運輸重量而定。 $ 100 +沒有運費,訂單少於100美元運費如下: 超過40磅:每磅1.09美元 超過20磅:每磅0.99美元 等於或低於20磅:每磅0.89美元

如果訂單價值是100美元+重量是不相關的,你的程序不應該要求它。

到目前爲止,這是我:

#Have user enter weight of order 
weight = int(input('Enter the weight of your order:')) 

#Determine shipping cost 
if weight >= 40: 
    total = weight * 1.09 
elif weight >= 20: 
    total = weight * 0.99 
elif weight <=20: 
    total = weight * 0.89 

#does shipping apply 
if total >= 100.00 
    else: 
if total <= 100.00 

,我不知道到什麼地方去,此地要放什麼東西在別人串並出貨申請下。

回答

1

您不應該使用else,只需print。或者您可以使用:

if total >= 100.00: 
    print() 
else: 
+0

因此,通過在括號中不加任何內容,它會自動跳到下一個,如果?如果是這樣,有沒有辦法在這裏提問? – mrsga16eats

1

您應該先檢查訂單的價值。正如你所說,如果它超過100美元,那麼重量並不重要,因爲它無論如何都是免費送貨。您可能已經從程序中的其他地方獲得了該號碼,因此只需檢查該值即可。然後,如果有必要,您可以繼續檢查體重。

# value is defined earlier in the program, and contains the total cost of the purchase 
if value >= 100: 
    print "You quality for free shipping!" 
else: 
    weight = int(input("Enter the weight of your order: ")) 
    # Now determine shipping cost 
    if weight > 40: 
     total = weight * 1.09 
    elif weight > 20: 
     total = weight * 0.99 
    else: # because there should only be weights equal to or below 20 remaining 
     total = weight * 0.89 

現在您可以向用戶顯示總數或執行任何其他您想要的操作。