-4

Python 2.7.5を使用して、注文合計が送料に追加されるプログラミングクラスの問題を解決し、価格は米国とカナダへの配送に設定されています。米国への配送用にコードを記述しましたが、値を変更する必要があるため、ユーザーがカナダを選択した場合注文合計には異なる値のセットが適用されます。カナダの送料は次のとおりです: 50.0 未満の場合は 8.00、50.01 を超える場合は 12.0 - 100.0、100.01 - 150.0 の場合は 15.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

4 に答える 4

0

ブール式が正しくありません。Python はそれらを自然に連鎖させるので、そのように書かなければなりません。

 50.01 < user_order < 100.0
于 2013-10-14T02:47:48.453 に答える
-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
于 2013-10-14T03:03:54.403 に答える