2013-10-14 34 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