22

送料を計算するプログラムをPythonで作成しようとしています。

ただし、プログラムを正常に動作する場所まで実行できません。

私の合計がどうであれ、同じ金額は、米国で 6 ドル、カナダで 8 ドルになります。私はそれを渡すことができないようです。

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

10 に答える 10

7

文字列を比較するときは、電話帳のように辞書順で比較します。例えば:

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

数を数えた順に比較したい場合は、int 型を使用する必要があります。

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

次に、コード内のすべての文字列リテラルを のような"50"整数リテラルに変更します50

于 2013-10-15T01:10:59.067 に答える
3

リンゴと家を足して合計を求めるようなものですが、これは不可能です。合計を取得するには、同じ型 (この場合は整数型) である必要があります。文字列を整数に変換するには、int() を使用します。

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

次のようにすることもできます (ただし、あまり好ましくありません):

 total = raw_input('What is the total amount for your online shopping?')
 total = int(total)
于 2016-06-19T06:18:09.023 に答える
-1

私はこことPythonプログラミングの初心者です。私はあなたの問題を解決しようとしていました。うまくいけば、これがあなたを助けることができます。

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')
于 2016-10-12T06:07:26.097 に答える