我正在尝试使用python创建一个计算运费的程序。
但是,我无法将程序运行到正常工作的位置。
我的总金额相同,美国为6美元,加拿大为8美元。我似乎无法通过那个。
total = raw_input('What is the total amount for your online shopping?')
country = raw_input('Shipping within the US or Canada?')
if country == "US":
if total <= "50":
print "Shipping Costs $6.00"
elif total <= "100":
print "Shipping Costs $9.00"
elif total <= "150":
print "Shipping Costs $12.00"
else:
print "FREE"
if country == "Canada":
if total <= "50":
print "Shipping Costs $8.00"
elif total <= "100":
print "Shipping Costs $12.00"
elif total <= "150":
print "Shipping Costs $15.00"
else:
print "FREE"
答案 0 :(得分:21)
int()
。integer
。是固定代码。
total = int(raw_input('What is the total amount for your online shopping?'))
country = raw_input('Shipping within the US or Canada?')
if country == "US":
if total <= 50:
print "Shipping Costs $6.00"
elif total <= 100:
print "Shipping Costs $9.00" # improved indentation
elif total <= 150:
print "Shipping Costs $12.00" # improved indentation
else:
print "FREE"
if country == "Canada":
if total <= 50:
print "Shipping Costs $8.00"
elif total <= 100:
print "Shipping Costs $12.00"
elif total <= 150:
print "Shipping Costs $15.00"
else:
print "FREE"
答案 1 :(得分:11)
你不能用数字比较字符串。而是首先转换为int然后进行比较。
例如:
if int(total) < 50
避免重复的变量也会有所帮助。
答案 2 :(得分:5)
您正在以数字方式比较字符串。这是不可能的,例如将apple
与orange
进行比较,哪一个更大?计算机无法理解,它需要比较大小。
为此,我们需要将其转换为整数。使用int()
功能。这里:
#convert it to an integer straight away
total = int(raw_input('What is the total amount for your online shopping?'))
country = raw_input('Shipping within the US or Canada?')
if country == "US":
if total <= 50:
print "Shipping Costs $6.00"
elif total <= 100:
print "Shipping Costs $9.00"
elif total <= 150:
print "Shipping Costs $12.00"
else:
print "FREE"
if country == "Canada":
if total <= 50:
print "Shipping Costs $8.00"
elif total <= 100:
print "Shipping Costs $12.00"
elif total <= 150:
print "Shipping Costs $15.00"
else:
print "FREE"
希望这有帮助!
答案 3 :(得分:5)
比较字符串时,它按字典顺序排列,就像在电话簿中一样。例如:
"a" < "b"
:是的
"bill" < "bob"
:是的
"100" < "3"
:是的
如果要按照我们计算的顺序比较数字,则需要使用int类型。
total = int(raw_input('What is the total amount for your online shopping?'))
然后将代码中的所有字符串文字(如"50"
)更改为50
等整数文字。
答案 4 :(得分:2)
此:
total = raw_input('What is the total amount for your online shopping?')
生成一个字符串。字符串和数字之间的比较定义不明确。您需要先将总数转换为数字。例如:
total = int(raw_input('What is the total amount for your online shopping?'))
(这会忽略输入错误处理,例如当用户的输入不是数字时)
请注意,Python 2.x和Python 3.x中的行为发生了变化。在Python 2.x:
在Python 3.x中不同类型的对象,除了不同的数字类型和不同的字符串类型,从不比较相等;这些对象是一致但任意排序的(因此排序异构数组会产生一致的结果)。
...
CPython实现细节:除了数字之外的不同类型的对象按其类型名称排序;不支持正确比较的相同类型的对象按其地址排序。
:
不同类型的对象(不同的数字类型除外)从不相等。
答案 5 :(得分:2)
使用raw_input时,您的用户输入以字符串形式出现,您无法以字符串格式计算数字。因此,您需要将字符串输入更改为整数才能进行比较。 你可以这样做:
total = int(raw_input('What is the total amount for your online shopping?'))
country = raw_input('Shipping within the US or Canada?')
if country == "US":
if total <= 50:
print "Shipping Costs $6.00"
elif total <= 100:
print "Shipping Costs $9.00"
elif total <= 150:
print "Shipping Costs $12.00"
else:
print "FREE"
elif country == "Canada":
if total <= 50:
print "Shipping Costs $8.00"
elif total <= 100:
print "Shipping Costs $12.00"
elif total <= 150:
print "Shipping Costs $15.00"
else:
print "FREE"
else:
print "Try Again"
答案 6 :(得分:1)
这就像添加苹果&amp;房子得到的总数是不可能的。它必须是相同的类型,在这种情况下是整数类型,以获得总数。使用int()将字符串转换为整数。
total = int(raw_input('What is the total amount for your online shopping?'))
也可能(但不太可取):
total = raw_input('What is the total amount for your online shopping?')
total = int(total)
答案 7 :(得分:1)
输入返回字符串
如果应该合计返回用于数学运算的输入,则应浮动输入
总计=(raw_input('您的网上购物总金额是多少?') 总计=浮动(总计)
答案 8 :(得分:1)
删除 if 语句中整数的引号,例如:
if total <= "50" -------> if total <= 50
答案 9 :(得分:-1)
我在这里只是一个更新鲜和python编程。我试图解决你的问题。希望,这个可以帮到你。
if country == 'US':
if total <= 50:
print ('Shipping Costs $6.00')
elif total <= 100:
print ('Shipping Costs $9.00')
elif total <= 150:
print ('Shipping Costs $12.00')
else:
print ('FREE')
elif country == 'Canada':
if total <= 50:
print ('Shipping Costs $8.00')
elif total <= 100:
print ('Shipping Costs $12.00')
elif total <= 150:
print ('Shipping Costs $15.00')
else:
print ('FREE')
else:
print ('Country name is case sensetive so do it perfectly')