我已经通过C ++学习,现在正在学习Python。关于变量在这里发生的事情,我有点困惑。我觉得'就像我应该宣布" wt"作为Real或Float,但它不会接受这种语法。我认为错误就在我宣布模块" calcAndDisplayShipping"。
该计划的目标是根据输入重量计算价格。
#main module
def main():
#local variables
weight = 0.0
#get package weight
weight = input("Enter the weight of your package: ")
#call module to calculate and display shipping charges
calcAndDisplayShipping (weight)
#module for calculating and displaying shipping charge
def calcAndDisplayShipping (wt):
#named constants for rates
underTwo = 1.10
twoToSix = 2.20,
sixToTen = 3.70
overTen= 3.80
#Local Variable
shipping = 0.0
#calculate charges
if wt > 10.0:
shipping = wt * overTen
elif wt > 6.0:
shipping = wt * sixToTen
elif wt > 2.0:
shipping = wt * twoToSix
else:
shipping = wt * underTwo
#display shipping charge
print ("Shipping charge for this package is: $", shipping)
#return to main
main()
我得到的错误是...... TypeError:'>' ' str'的实例之间不支持并且'浮动'
我搜索了我的语言伴侣python并且找不到任何帮助。
答案 0 :(得分:0)
在Python 3中,input
返回一个字符串。要获得浮动,您应该将此调用包装在float()
。
weight = input("Enter the weight of your package: ")
你可能会遇到另一个问题,因为这是当前编写的,你永远不会退出函数调用 - 你只会增加调用堆栈的大小。相反,您可能希望在main
的末尾删除对calcAndDisplayShipping
的调用,然后在main
中使用while循环。
def main():
#local variables
weight = 0.0
while True:
#get package weight
weight = float(input("Enter the weight of your package: "))
#call module to calculate and display shipping charges
calcAndDisplayShipping(weight)
答案 1 :(得分:0)
这里有一些问题。主要的一点是,在分配给weight
之前,不要将字符串转换为浮点数。你可以用:
weight = float(input(...
在此之前您无需设置weight = 0.0
。它被完全覆盖了。
其他问题是您的缩进是错误的并且“计算费用”片段在函数之外。它将作为模块代码块的一部分运行。
您在评论中的命名也不正确。你定义的是函数,而不是模块。 (main
,calcAndDisplayShipping
)最后,您正在调用main
,而不是返回它。
最后这不符合你的期望:
twoToSix = 2.20,
它定义了一个1元素元组,相当于twoToSix = (2.20,)
。您需要删除逗号才能获得数字。
答案 2 :(得分:0)
由于Python 3.x不评估和转换数据类型,您必须使用float显式转换为浮点数,如float(input("question:"))
试试这个:
def main():
weight = 0.0
weight = float(input("Enter the weight of your package: "))
#call module to calculate and display shipping charges
calcAndDisplayShipping (weight)
#module for calculating and displaying shipping charge
def calcAndDisplayShipping (wt):
#named constants for rates
underTwo = 1.10
twoToSix = 2.20,
sixToTen = 3.70
overTen= 3.80
#Local Variable
shipping = 0.0
#calculate charges
if wt > 10.0:
shipping = wt * overTen
elif wt > 6.0:
shipping = wt * sixToTen
elif wt > 2.0:
shipping = wt * twoToSix
else:
shipping = wt * underTwo
#display shipping charge
print ("Shipping charge for this package is: $", shipping)
#return to main
main()
答案 3 :(得分:0)
你的问题主要是缩进,你的变量wt必须在函数的缩进内,并且必须将输入转换为浮动变量,如下面的代码所示,我还删除了一个错误的逗号。并注意最后的main()调用函数main,但不返回任何内容。
#main module
def main():
#local variables
weight = 0.0
#get package weight
weight = float(input("Enter the weight of your package: "))
#call module to calculate and display shipping charges
calcAndDisplayShipping (weight)
#module for calculating and displaying shipping charge
def calcAndDisplayShipping (wt):
#named constants for rates
underTwo = 1.10
twoToSix = 2.20
sixToTen = 3.70
overTen = 3.80
#Local Variable
shipping = 0.0
#calculate charges
if wt > 10.0:
shipping = wt * overTen
elif wt > 6.0:
shipping = wt * sixToTen
elif wt > 2.0:
shipping = wt * twoToSix
else:
shipping = wt * underTwo
#display shipping charge
print ("Shipping charge for this package is: $", shipping)
# calls main function!!!!!
main()