简体   繁体   English

python if elif else语句

[英]python if elif else statement

I'm trying to create a program with python that calculate the cost for shipping.我正在尝试使用 python 创建一个程序来计算运输成本。

However, I can't run the program to where it works properly.但是,我无法将程序运行到正常运行的位置。

What ever my total is the same amount comes out as $6 for US and $8 for Canada.我的总金额与美国的 6 美元和加拿大的 8 美元相同。 I can't seem to get pass that.我似乎无法通过。

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"
  1. you should get integer from raw_input, not string.你应该从 raw_input 得到整数,而不是字符串。 use int() .使用int()
  2. comparison values like 50, 100, 150, ... also should be integer .像 50, 100, 150, ... 这样的比较值也应该是integer

below is fixed code.下面是固定代码。

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"

You can't compare Strings numerically.您不能以数字方式比较字符串。 Instead convert to an int first and then compare.而是先转换为 int 然后比较。

For example:例如:

if int(total) < 50

Variables to avoid duplication would help too.避免重复的变量也会有所帮助。

You are comparing strings numerically.您正在以数字方式比较字符串 That's impossible, like comparing apple with orange , which one is bigger?那是不可能的,就像比较appleorange ,哪个更大? The computer won't understand that, it needs to compare the size .计算机不会理解,它需要比较大小

To do that, we need to convert it to an integer.为此,我们需要将其转换为整数。 Use the int() function.使用int()函数。 Here:在这里:

#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"

Hope this helps!希望这有帮助!

When you compare strings it does so lexicographically, like in a phone book.当您比较字符串时,它会按字典顺序进行比较,就像在电话簿中一样。 For example:例如:

"a" < "b" : True "a" < "b" : 真
"bill" < "bob" : True "bill" < "bob" :真
"100" < "3" : True "100" < "3" :真

If you want to compare numbers in the order that we count them you need to use the int type.如果你想按照我们计数的顺序比较数字,你需要使用 int 类型。

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

Then change all of the string literals in your code like "50" to integer literals like 50 .然后将代码中的所有字符串文字(如"50"更改为整数文字(如50

This:这个:

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

produces a string.产生一个字符串。 Comparison between string and numbers are not very well defined.字符串和数字之间的比较没有很好地定义。 You need to convert total to a number first.您需要先将总数转换为数字。 Example:示例:

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

(this ignores input error handling such as when the user's input is not a number) (这会忽略输入错误处理,例如当用户输入不是数字时)

Note that the behavior changes in Python 2.x and Python 3.x.请注意,Python 2.x 和 Python 3.x 中的行为发生了变化。 In Python 2.x :Python 2.x 中

Objects of different types, except different numeric types and different string types, never compare equal;不同类型的对象,除了不同的数值类型和不同的字符串类型,从不比较相等; such objects are ordered consistently but arbitrarily (so that sorting a heterogeneous array yields a consistent result).这些对象的排序是一致但任意的(因此对异构数组进行排序会产生一致的结果)。

... ...

CPython implementation detail: Objects of different types except numbers are ordered by their type names; CPython实现细节:除数字外的不同类型的对象按类型名称排序; objects of the same types that don't support proper comparison are ordered by their address.不支持正确比较的相同类型的对象按其地址排序。

while in Python 3.x :Python 3.x 中

Objects of different types, except different numeric types, never compare equal.不同类型的对象,除了不同的数字类型,永远不会比较相等。

When using raw_input your user input comes in as a string and you cannot calculate numbers in the format of strings.使用 raw_input 时,您的用户输入以字符串形式出现,您无法以字符串格式计算数字。 So you need to change your string input to an integer in order to make the comparisons.因此,您需要将字符串输入更改为整数才能进行比较。 You can do like this:你可以这样做:

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"

It's like adding apples & houses to get the total which is impossible.这就像添加苹果和房屋来获得总数,这是不可能的。 It needs to be the same type, in this case integer type, to get the total.它需要是相同的类型,在这种情况下是整数类型,以获得总数。 Use the int() to convert the string into an integer.使用 int() 将字符串转换为整数。

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

could also be (but less preferable):也可以(但不太可取):

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

input returns a string输入返回一个字符串

if total is supposed to return an input for math operations then you should float the input如果 total 应该返回数学运算的输入,那么您应该浮动输入

total = (raw_input('What is the total amount for your online shopping?')) total = float(total) total = (raw_input('你网上购物的总金额是多少?')) total = float(total)

Remove quotations from integers in if statements such as:删除 if 语句中整数的引号,例如:

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

I am just a fresher here and python programming.我只是这里的新手和 python 编程。 I was trying to solve your problem.我试图解决你的问题。 Hope, this one could help you.希望,这个可以帮到你。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM