我正在嘗試使用 IF 陳述句在 python 中制作運費計算器。應用程式應計算訂單的總成本,包括運費。我被困在python根本不會運行我的代碼的地步。我感覺我很接近但我不知道我輸入的內容有什么問題,因為 python 不會回傳任何錯誤。請有人引導我朝著正確的方向前進。
指示:
創建一個計算訂單總成本(包括運費)的程式。
當您的程式運行時,它應該...
- 列印“運費計算器”程式的名稱
- 提示用戶輸入訂購商品的成本
- 如果用戶輸入的數字小于零,則顯示錯誤訊息并讓用戶有機會再次輸入該數字。(注意:如果您知道回圈/迭代,可以在這里使用,但如果不知道,只需使用 if 陳述句進行簡單的一次性檢查即可。)例如:如果成本小于零,則列印錯誤訊息,然后重新詢問一次輸入。
使用下表計算運費: | 物品成本 | 運費 | | ------------- | ------------- | | <30 | 5.95 | | 30.00-49.99 | 7.95 | | 50.00-74.99 | 9.95 | | >75.00 | 免費 |
- 列印運輸成本和總成本以運送物品
- 以您選擇的退出問候結束程式(例如,感謝您使用我們的運費計算器!)
示例 1:
================= 運費計算器
訂購商品的成本:49.99
運費:7.95
總成本:57.94
感謝您使用我們的運費計算器!
示例 2:
================= 運費計算器
訂購商品的成本:-65.50
您必須輸入一個正數。請再試一次。
訂購物品的成本:65.50
運費:9.95
總成本:75.45
感謝您使用我們的運費計算器!
這是我到目前為止寫的代碼:
#Assignment shipping calculator
#Author: Name
def main():
print("Shipping Calculator")
subtotal = calc_subtotal()
shipping = calc_shipping()
total = calc_total()
main()
def calc_subtotal():
subtotal = float(input("Cost of Items Ordered: $"))
if subtotal <= 0:
print("You must enter a positive number. Please try again.")
return subtotal
def calc_shipping():
subtotal = calc_subtotal()
shipping = calc_shipping
if subtotal >= 1 and subtotal <= 29.99:
shipping = 5.95
print("Shipping Cost: $5.95")
if subtotal >= 30 and subtotal <= 49.99:
shipping = 7.95
print("Shipping Cost: $7.95")
if subtotal >= 50 and subtotal <= 74.99:
shipping = 9.95
print("Shipping Cost: $9.95")
if subtotal >= 75:
shipping = 0
print("Shipping Cost: Free")
def calc_total():
total = calc_subtotal() calc_shipping()
print("Total Cost: ",total)
print("Thank you for using our shipping calculator!")
uj5u.com熱心網友回復:
首先,我將代碼放在這里,然后解釋我所做的所有更改(您最初的嘗試非常接近,只是一些調整):
#Assignment shipping calculator
#Author: Name
def main():
print("Shipping Calculator")
subtotal = calc_subtotal()
shipping = calc_shipping(subtotal)
calc_total(subtotal,shipping)
def calc_subtotal():
subtotal = float(input("Cost of Items Ordered: $"))
while subtotal <= 0:
print("You must enter a positive number. Please try again.")
subtotal = float(input("Cost of Items Ordered: $"))
return subtotal
def calc_shipping(subtotal):
if subtotal >= 1 and subtotal <= 29.99:
shipping = 5.95
print("Shipping Cost: $5.95")
if subtotal >= 30 and subtotal <= 49.99:
shipping = 7.95
print("Shipping Cost: $7.95")
if subtotal >= 50 and subtotal <= 74.99:
shipping = 9.95
print("Shipping Cost: $9.95")
if subtotal >= 75:
shipping = 0
print("Shipping Cost: Free")
return shipping
def calc_total(subtotal,shipping):
total = subtotal shipping
print("Total Cost: $" str(round(total,2)))
print("Thank you for using our shipping calculator!")
main()
正如@Devang Sanghani 提到的,您應該呼叫您
main()的函式外部以便它運行對于您的
calc_subtotal()函式,使用 while 回圈繼續接收用戶輸入,直到給出有效數字。確保將您移出return subtotal此回圈,以便僅在此數字有效時才回傳它。在您的
calc_shipping()函式中,請確保您將subtotal其作為引數,因為您在函式中使用它。確保你也回傳shipping。同樣,在您的
calc_total()函式中,將subtotal和shipping作為引數,因為它們在您的函式中使用。鑒于這些更改,請
main()相應地更新您的函式。
我希望這些改變是有意義的!如果您需要任何進一步的幫助或澄清,請告訴我:)
uj5u.com熱心網友回復:
所以似乎有2個錯誤:
- 你忘了呼叫主函式
- 您縮進了 return 陳述句,
calc_subtotal()因此只有當subtotal <= 0正確的代碼可能是:
def main():
print("Shipping Calculator")
subtotal = calc_subtotal()
shipping = calc_shipping()
total = calc_total()
def calc_subtotal():
subtotal = float(input("Cost of Items Ordered: $"))
if subtotal <= 0:
print("You must enter a positive number. Please try again.")
calc_subtotal() # Re asking for the input in case of invalid input
else:
return subtotal # returning subtotal in case of valid input
def calc_shipping():
subtotal = calc_subtotal()
shipping = calc_shipping
if subtotal >= 0 and subtotal <= 29.99:
shipping = 5.95
print("Shipping Cost: $5.95")
if subtotal >= 30 and subtotal <= 49.99:
shipping = 7.95
print("Shipping Cost: $7.95")
if subtotal >= 50 and subtotal <= 74.99:
shipping = 9.95
print("Shipping Cost: $9.95")
if subtotal >= 75:
shipping = 0
print("Shipping Cost: Free")
def calc_total():
total = calc_subtotal() calc_shipping()
print("Total Cost: ",total)
print("Thank you for using our shipping calculator!")
main() # Calling main
uj5u.com熱心網友回復:
除了上面的答案,還要考慮價格可能是幾美分的情況,所以這條線應該改變:
if subtotal >= 0 and subtotal <= 29.99: # changed from 1 to 0
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/432498.html
標籤:Python python-3.x if 语句 计算器 航运
