我做这个练习,计算寄一个小包裹的费用。Te邮局对前300克收取R5,之后每100克收取R2(四舍五入),最高不超过1000克。你知道吗
weight = raw_input("What are the weight of you parcel: ")
if weight <= 1000:
if weight <= 300:
cost = 5
print("You parcel cost: " + cost)
else:
cost = 5 + 2 * round((weight - 300)/ 100)
print("You parcel cost: " + cost)
else:
print("Maximum weight for amall parcel exceeded.")
print("Use large parcel service instead.")
当我执行空闲控制台时,我只执行最后的else语句。你知道吗
weight
在第1行变成string type
,然后在if statement
中比较weight
和int
。通过将用户输入转换为int来修复此问题将第一行更改为:
另外,如果您使用python3,我会将
raw_input
更改为input
将
weight
强制转换为int,weight = int(weight)
。现在它是一个字符串,与1000相比,它的计算结果总是False
。你知道吗首先,你有压痕问题。第二,比较字符串和int。然后,比较。。。你知道吗
你应该自己检查,但是
round(0) = 0
,和round(0.5) = 1
。你知道吗下面是解决问题的代码
相关问题 更多 >
编程相关推荐