2013-10-14 31 views
-4

使用python 2.7.5,解决编程类问题,其中订单总额被添加到运输成本,价格被设置为运送到美国和加拿大,我写有代码运送到美国但需要更改值,因此如果用户选择加拿大,则将不同的一组值应用于订单总额。 加拿大运费:8.00小于50.0,12.0超过50.01 - 100.0,15.0 100.01 - 150.0,我需要一种方法,在下面的代码在美国国内运费替换这些值:python多if/elif/else语句并仅显示用户选择

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. 
+0

请正确缩进您的代码。 – thefourtheye

回答

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. 无论何时您发布代码(甚至更好,写作时)尝试并正确缩进代码。这将使调试更容易。

+0

在Python 2中使用'input'是definitley的一个坏主意。 “eval”的内部使用不是一个优势,而是一个安全漏洞。 – Matthias

+0

@Matthias谢谢。忘了提到这一点。现在,我将其包含在答案中。 – thefourtheye

0

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

50.01 < user_order < 100.0 
+0

值得注意的是,在OPs代码中的比较是有效的,只是没有做他们所期望的。 'user_order < 100.0 > 50.01'将被分解为'user_order <100.0 && 100.0> 50.01',它等于'user_order <100。0 && True' Plus,因为python条件会从头到尾被解释,即使表达式很奇怪,它们仍然是正确的。 – 2013-10-14 02:56:34

+0

@LegoStormtroopr'&&'不是Python中的合法运算符 – SethMMorton

-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) 
+1

你能解释一下你“如何清理它”吗? – 2013-10-14 02:54:01

+1

你拥有的布尔表达式并没有达到你期望的效果 – SethMMorton

-1

这是正确的形式,请注意如何把条件放在if-else在Python中。

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 
+0

这并没有解决逻辑重复两次的问题,并且关于python如何链接布尔运算符是错误的。 – 2013-10-14 03:36:15