2017-12-04 4 views
3

を働いていない:のpythonでの私の乗算は、私は以下のコードに取り組んforループ内の乗算を取得しようとしている

#products list and it's values per unity 
tomato = 2 
potato = 3 
carrot = 1 
pricePaid = int() 

print("Welcome to my grocery shop!\n") 

productList = ["Potato", "Tomato", "Carrot"] 

print("What will you want today?\n""We have:") 
print(*productList, sep=', ') 
product = input("What will it be? ") 

quantity = int(input("And how many do you want? ")) 

for productChoice in product: 
    if productChoice == "Potato": 
     pricePaid = quantity * potato 
    elif productChoice == "Tomato": 
     pricePaid = quantity * tomato 
    elif productChoice == "Carrot": 
     pricePaid = quantity * carrot 

print("Here's your bag with {0} {1}s. The total is ${2:.2f}.".format(quantity, product, pricePaid)) 

が、私はそれは量であるべきところ私の結果として$ 0.00取得しています* productPrice:

"Here's your bag with 1 Tomatos. The total is $0.00." 

なぜ失敗しますか?私はここで何が欠けていますか?

私は

事前のおかげでPythonの3.xを使用しています!

+1

私は '製品のために選択してください:'とは思っていません。 'product'は文字列(' input'は文字列を返します)であるため、 'productChoice'はあなたの入力のすべての文字になります。 –

答えて

2

あなたは入力の文字を繰り返し、毎回支払った価格を設定しています。 productは変更されないため、反復する必要はありません。そのforループを削除すると、そのトリックが実行されます。 productChoiceへの参照を削除する必要もあります。なぜなら、(文字通り)目的がないからです。

#products list and it's values per unity 
tomato = 2 
potato = 3 
carrot = 1 
pricePaid = int() 

print("Welcome to my grocery shop!\n") 

productList = ["Potato", "Tomato", "Carrot"] 

print("What will you want today?\n""We have:") 
print(*productList, sep=', ') 
product = input("What will it be? ") 

quantity = int(input("And how many do you want? ")) 

if product == "Potato": 
    pricePaid = quantity * potato 
elif product == "Tomato": 
    pricePaid = quantity * tomato 
elif product == "Carrot": 
    pricePaid = quantity * carrot 

print("Here's your bag with {0} {1}s. The total is ${2:.2f}.".format(quantity, product, pricePaid)) 
+0

ありがとう、@jhpratt。それは役に立ちます。 – RGregg

1

あなたは手紙producChoiceを反復処理しています。

ループにprint(productChoice)を追加すると、何が起こるかを見ることができます。 あなたの製品の1文字が一文字にはならないので、条件文のいずれもトリガーされず、pricePaidは元の値のint() == 0に留まります。

ここではループは一切不要なので、削除してください。

1

ここでは、辞書を使用した改良されたコードです。辞書だけではコードをもっと軽くするために勉強する価値があります。基本的に辞書は、値(例えば1)を保持するキー(例えばトマト)によって構成される。これらの値は、.get()を使用して取得できます。

for productChoice in product: 
    if productChoice == "Potato": 
     pricePaid = quantity * potato 
    elif productChoice == "Tomato": 
     pricePaid = quantity * tomato 
    elif productChoice == "Carrot": 
     pricePaid = quantity * carrot 

我々はこれに素早く、我々は製品が「トマトであることをものにする理由

for productChoice in product: 
    print(productChoice) 

出力を見ることができることを変更した場合:これはあなたの問題を引き起こしている

#products list and it's values per unity 
prices = dict(tomato=2,potato=3,carrot=1) 

product = input('''\ 
Welcome to my grocery shop! 
What will you want today? 
We have: {} 
What will it be? 
'''.format(', '.join(prices.keys()))).lower() 

quantity = int(input("And how many do you want? ")) 

pricePaid = prices.get(product,0) * quantity 

if pricePaid: 
    print("Here's your bag with {0} {1}s. The total is ${2:.2f}." 
      .format(quantity, product, pricePaid)) 
else: 
    print("Error with input!") 

+1

これはうまくいくかもしれませんが、コードを手元に完全に変更して、ポスターが失敗したコードを理由に何も助けてくれません。 – jhpratt

+1

しかし、誰があなたの書き直しでコードが何をしているのか理解しているとは誰でしょうか? – jhpratt

+0

ありがとう、@AntonvBR。それは私のコードがどのように書き直されたのかを知るのに役立ちます。私は理解していますが、このコンセプトを吸収するにはしばらく時間がかかります。私は本当にあなたの答えをありがとう! – RGregg

2

"

T 
o 
m 
a 
t 
o 

ここでは、実際にこの動作が必要ないときに、文字列productの各文字を繰り返し処理しています。問題を修正するには、forループを削除して、選択範囲のみを保持します。

これはあなたが必要とするすべてである:

if product == "Potato": 
    pricePaid = quantity * potato 
elif product == "Tomato": 
    pricePaid = quantity * tomato 
elif product == "Carrot": 
    pricePaid = quantity * carrot 

は、この情報がお役に立てば幸い!

関連する問題