簡體   English   中英

如何在 if 語句中使用多個輸入

[英]how to use multiple inputs with if statements

我是 python 新手,所以這可能非常簡單,但是,我想讓用戶寫下包裹的重量。 套餐1,我們可以稱之為。 然后用戶可以寫另一個包裹的重量,等等。最后,代碼把價格加在一起,寫出總價格。

這是我的代碼。 它僅適用於 1 個包裹。 不知道如何超過 1 個包裹。

def package_price(kg):
    if kg < 0:
        return "The number is negative "
    if kg <= 10:
        return "That would be 149kr"
    elif kg <= 25:
        return "That would be 268kr"
    elif kg <= 35:
        return "That would be 381kr"
    else:
        return "The package is too heavy"


weight = input("How much does the package weight?: ")
kg = int(weight)
package = package_price(kg)
print(package)

您需要兩個輸入語句,然后將結果相加。

weight1 = int(input("How much does the first package weight?: "))
price1 = package_price(weight1)

weight2 = int(input("How much does the second package weight?: "))
price2 = package_price(weight2)

print(price1 + price2)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM