Python的if elif else语句

21

我正在尝试使用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"

你需要正确格式化你的代码...尝试缩进它4个空格。 - Greg
6
如果购买金额超过150美元,免费送货是否是有意为之? - SethMMorton
这个问题很糟糕,我不明白为什么它会有10万次的浏览量和积极的反响。问题被明显地错误识别了,问题标题因此具有误导性,并且没有尝试创建一个适当的[mre]或者debug来独立进行简单的调试。它可能已经成为“如何比较表示数字的字符串”的规范问题;但需要这些信息的人永远找不到它。链接的重复问题是我们拥有的最接近的适当问题。 - Karl Knechtel
10个回答

23
  1. 你应该从raw_input中获取整数而不是字符串,使用int()
  2. 像50、100、150这样的比较值也应该是整数

以下是修正后的代码。

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"

total = int(raw_input('您的网购总金额是多少?'))并将几个字符串更改为整数。 - Curry
与其只贴代码,提供问题和解决方案的实际描述会更好。 - Graeme Perrow
@Curry 非常感谢您! - Sohan Das

12

你不能通过数值比较字符串。相反,先将其转换为整数,然后再进行比较。

例如:

if int(total) < 50

避免重复的变量也会有所帮助。


int("5") 看起来像是用冗长的方式表达 5 - DSM
好的。但在实际代码中,total是一个变量,所以需要int(total)。啊,你的意思是应该是if int(total) < 50。我会编辑以提高清晰度。 - Jeanne Boyarsky
我认为将total转换为int(或者,在我看来,是float)一次,而不是为每个比较重复转换,会更有意义。 - DSM
我也是。因此我评论了变量以避免重复。 - Jeanne Boyarsky

7
你正在进行字符串的数字比较。这是不可能的,就像比较“苹果”和“橙子”,哪一个更大?计算机无法理解,它需要比较大小。
为了实现这一点,我们需要将其转换为整数。使用“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"

希望这可以帮助到您!

7
当你比较字符串时,它是按字典顺序比较的,就像在电话簿中一样。例如:
"a" < "b":True
"bill" < "bob":True
"100" < "3":True
如果您想按我们数数的顺序比较数字,您需要使用int类型。
total = int(raw_input('What is the total amount for your online shopping?'))
然后将代码中所有的字符串文字,如"50"更改为整数文字,如50

4

使用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"

4

This:

total = raw_input('What is the total amount for your online shopping?')

生成一个字符串。在字符串和数字之间的比较没有很好的定义。您需要先将 total 转换为数字。例如:

total = int(raw_input('What is the total amount for your online shopping?'))

(这忽略了输入错误处理,例如当用户的输入不是数字时)

请注意,Python 2.x和Python 3.x中的行为有所变化。在Python 2.x中:

不同类型的对象(除了不同的数值类型和不同的字符串类型)永远不会相等;这样的对象被一致而任意地排序(因此对异构数组进行排序会产生一致的结果)。

...

CPython实现细节:不同类型的对象(除了数字)按其类型名称排序;不支持适当比较的相同类型的对象按其地址排序。

而在Python 3.x中:

不同类型的对象(除了不同的数值类型)永远不会相等。


3

这就像把苹果和房子加起来求总数一样,是不可能的。必须保证它们是同一种类型,这里是整数类型,才能得到总数。使用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)

2

输入返回一个字符串

如果total应该返回一个用于数学运算的输入,则应将输入转换为浮点数。

total = (raw_input('您的在线购物总金额是多少?')) total = float(total)


1

从if语句中删除整数的引号,例如:

if total <= "50" -------> if total <= 50


看起来这个答案已经被回答了。如果你有什么要补充的,请相应地编辑你的帖子。 - Rishabh Kumar

-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')

指出你所做的更改。在Python中,要正确缩进,否则程序可能会因此出错。此外,在这种情况下,你只更改了print行,这似乎可能有助于根据Python版本进行调试,但并不是解决问题的答案。 - Risadinha

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接