使用IF语句的Python运输计算器

yhuiod9q  于 2023-01-14  发布在  Python
关注(0)|答案(3)|浏览(152)

我试图使一个航运计算器在python使用IF语句。该应用程序应计算总成本的订单,包括航运。我被困在一个点,python只是不会运行我的代码在所有。我有一种感觉,我接近,但我不知道什么是错误的,我正在输入的,因为python不会返回任何错误。请有人只是引导我在正确的方向。
说明:
创建一个程序来计算包括运费在内的订单总成本。
当你的程序运行时,它应该...
1.打印程序的名称"航运计算器"
1.提示用户键入订购项目的成本
1.如果用户输入的数字小于零,则显示错误消息并让用户有机会重新输入数字。(注意:如果您知道循环/迭代,可以在此使用,但如果不知道,只需使用if语句进行简单的一次性检查即可。)例如:如果成本小于零,则打印错误消息,然后重新要求输入一次。
使用下表计算运费:
| 项目成本|运费|
| - ------|- ------|
| 30岁以下|五块九五|
| 30.00-49.99|七点九五|
| 50.00-74.99|九点九五分|
| 大于75时|自由|
1.打印发运项目的运费和总成本
1.以您选择的退出问候语结束程序(例如,感谢您使用我们的运费计算器!)
例一:

=================
Shipping Calculator
=================
Cost of items ordered: 49.99

Shipping cost:                     7.95

Total cost:                          57.94

Thank you for using our shipping calculator!

例二:

=================
Shipping Calculator
=================
Cost of items ordered: -65.50

You must enter a positive number. Please try again.

Cost of items ordered: 65.50

Shipping cost:                   9.95

Total cost:                         75.45

Thank you for using our shipping calculator!

下面是我到目前为止编写的代码:

#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!")
py49o6xq

py49o6xq1#

首先,我将把代码放在这里,然后解释我所做的所有更改(您最初的尝试非常接近,只是一些调整):

#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()

1.正如@ DevangSanghani提到的,应该在函数外部调用main(),以便它能够运行
1.对于calc_subtotal()函数,使用while循环接收用户输入,直到给定一个有效的数字。确保将return subtotal移出此循环,以便它只在此数字有效时返回它。
1.在calc_shipping()函数中,确保将subtotal作为参数,因为您在函数中使用它。确保还返回shipping
1.类似地,在calc_total()函数中,将subtotalshipping都作为参数,因为它们在函数中使用。
1.考虑到这些更改,请相应地更新main()函数。
我希望这些改变是有意义的!如果你需要任何进一步的帮助或澄清,请让我知道:)

lf5gs5x2

lf5gs5x22#

除了上面的答案,还要考虑价格可能是几美分的情况,所以这行应该改变:

if subtotal >= 0 and subtotal <= 29.99: # changed from 1 to 0
t9aqgxwy

t9aqgxwy3#

因此,似乎存在2个错误:
1.您忘记调用main函数
1.您缩进了calc_subtotal()中的return语句,以便它仅在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

相关问题