def main():
userInput()
calculate()
def userInput():
print("Please put in the weight of your package:")
a= input()
weight= float(a)
def calculate():
if weight <= 2:
print('Your rate is $1.10')
elif weight > 2 or weight <= 6:
print('Your rate is $2.20')
elif weight > 6 or weight <= 10:
print('Your rate is $3.70')
else:
print('Your rate is $3.80')
main()
所以基本上我想知道如何在“计算”模块中使用“userInput”模块中的数据。我知道我有一个通过论证,但是(这让我疯了)因为我的生活我无法找到正确的方法来做到这一点。我理解参数的概念,但我无法在工作中的代码中实现它。感谢。
答案 0 :(得分:0)
您可以将weight
作为参数传递
def userInput():
a= input("Please put in the weight of your package:")
weight = None
try:
weight= float(a)
except:
userInput()
return weight
def calculate(weight):
if weight <= 2:
print('Your rate is $1.10')
elif weight > 2 and weight <= 6:
print('Your rate is $2.20')
elif weight > 6 and weight <= 10:
print('Your rate is $3.70')
else:
print('Your rate is $3.80')
def main():
weight = userInput()
calculate(weight)
main()
答案 1 :(得分:0)
作为旁白你可以用:
重写体重检查if weight <= 2:
print('Your rate is $1.10')
elif 2 < weight <= 6:
print('Your rate is $2.20')
elif 6 < weight <= 10:
print('Your rate is $3.70')
else:
print('Your rate is $3.80')
注意使用“n&lt; x&lt; n +”符号
在shantanoo发表评论后更新:
我正在查看原始问题中的代码:
def calculate():
if weight <= 2:
print('Your rate is $1.10')
elif weight > 2 or weight <= 6:
print('Your rate is $2.20')
elif weight > 6 or weight <= 10:
print('Your rate is $3.70')
else:
print('Your rate is $3.80')
我意识到在两个elif行上,不需要进行第一次比较,代码可以重写为:
def calculate():
if weight <= 2:
print('Your rate is $1.10')
elif weight <= 6:
print('Your rate is $2.20')
elif weight <= 10:
print('Your rate is $3.70')
else:
print('Your rate is $3.80')