Python多个if / elif / else语句并仅显示用户选择

时间:2013-10-14 02:43:11

标签: python

使用python 2.7.5,解决编程类问题,其中订单总额被添加到运费中,价格设置为运往美国和加拿大,我有编写的代码用于运送到美国但需要更改值,所以如果用户选择加拿大,将一组不同的值应用于订单总计。 加拿大运费是:8.00表示低于50.0,12.0表示超过50.01 - 100.0,15.0表示100.01 - 150.0,我需要一种方法用以下代码中的美国运输代替这些数值:

user_order = float(input("how much is your order? "))
user_ship_area = raw_input('Are you shipping to the US or Canada?')

if user_order < 50.00 and user_ship_area == "US": #start selection with user order and shipping area. 
    #User would be given price for shipping according to order cost   
    print 'you must pay $6.00 for shipping ' #user order less than 50$ = 64 for shipping.
    user_order = user_order + 6.0

elif user_order < 100.0 > 50.01 and user_ship_area == "US": #must include range of numbers, if user order is over 50.01 but under 100.0 
    print 'you must pay $9.00 for shipping'   #they must pay 9.00 for shipping.
    user_order = user_order + 9.0

elif user_order < 150.0 > 100.01 and user_ship_area == "US": #if user order is over 100.01$ but under 150$ 
    print 'you must pay $12.00 for shipping '#12.00$ will be charged for shipping
    user_order = user_order + 12.0 
else: 
    print 'congratulations you qualify for free shipping' #since it works by checking each elif, it goes to else


print "your order total is", user_order 

#need to create option for shipping to Canada, the prices are different.
#how to repeat the above code but for different values if the user selects Canada. 

4 个答案:

答案 0 :(得分:1)

当您提供类似

的内容时
user_order < 100.0 > 50.01

到python它将像这样评估它

user_order < 100.0 and 100.0 > 50.01

但这不是我们想要的。我们想查看这个

user_order < 100.0 and user_order > 50.01

所以写条件的正确方法是

50.01 < user_order < 100.0

<强>建议:

  1. 你不必使用

    float(input("how much is your order? "))
    

    或者

    float(raw_input("how much is your order? "))
    

    input("how much is your order? ") # Don't use this
    

    因为input会在内部执行eval(whatever user inputs)。这将自动找到正确的数据类型。 注意绝不在Python2中使用input,这可能会导致潜在的安全问题。 (感谢Matthias指出来)

  2. 无论何时发布代码(写作时更好),请尝试正确缩进代码。这样可以更容易调试。

答案 1 :(得分:0)

您的布尔表达式不正确; Python自然地将它们链接起来,所以它们必须以这种方式编写。

 50.01 < user_order < 100.0

答案 2 :(得分:-1)

看起来程序重复了两次。我清理了一下,这很有效:

user_order = float(input("how much is your order? ")) 
total = user_order

if user_order < 50.00:
    #User would be given price for shipping according to order cost   
    print 'you must pay $6.00 for shipping ' 
    total = user_order + 6.0

elif user_order < 100.0 > 50.01: 
    print 'you must pay $9.00'   #they must pay 9.00 for shipping.
    total = user_order + 9.0

elif user_order < 150.0 > 100.01: #if user order is over 100.01$ but under 150$ 
    print 'you must pay $12.00 for shipping '#12.00$ will be charged for shipping
    total = user_order + 12.0 
else: 
    print 'congratulations you qualify for free shipping'                                
    #this is where the confusion occurs, I need to have the option to ship to US or Canada               
    #the values for shipping to Canada since its different for shipping to that country 

user_ship_area = raw_input('Are you shipping to the US or Canada?')
if user_ship_area != 'US':
    print 'confirmed, we will ship to Canada '

else:
    print "confirmed, we will ship to the United States"

print "your order total is %s"%(total)

答案 3 :(得分:-1)

这是正确的形式,请注意如何在python中将条件置于if-else之下。

user_order = float(input("how much is your order? ")) 

if user_order < 50.00:
    #User would be given price for shipping according to order cost   
    print 'you must pay $6.00 for shipping ' 
    user_order = user_order + 6.0

elif user_order < 100.0 and user_order > 50.01: 
    print 'you must pay $9.00'   #they must pay 9.00 for shipping.
    user_order = user_order + 9.0

elif user_order < 150.0 and user_order > 100.01: #if user order is over 100.01$ but under   150$ 
    print 'you must pay $12.00 for shipping '#12.00$ will be charged for shipping
    user_order = user_order + 12.0 
else: 
    print 'congratulations you qualify for free shipping'                                
#this is where the confusion occurs, I need to have the option to ship to US or Canada               
#the values for shipping to Canada since its different for shipping to that country 

user_ship_area = raw_input('Are you shipping to the US or Canada?')
if user_ship_area != 'US':
    print 'confirmed, we will ship to Canada '

else:
    print "confirmed, we will ship to the United States"
    print "your order total is", user_order 

if user_order < 50.00:
#User would be given price for shipping according to order cost   
    print 'you must pay $8.00 for shipping '
    user_order = user_order + 8.0 

elif user_order < 100.0 and user_order > 50.01: 
    print 'you must pay $12.00' 
    user_order = user_order + 12.0 

elif user_order < 150.0 and user_order > 100.01: 
    print 'you must pay $15.00 for shipping '
    user_order = user_order + 15.0 
else: 
    print 'congratulations you qualify for free shipping' 
#the final output needs to be the user_order + the amount its shipping to depending on   
#the country