python - can't multiply sequence by non-int of type 'str' Dont understand -


yen = 0.0067 bsp = 1.35 usd = 0.65 ero = 0.85  if choice == "2":     current_currency = input("what currency want exchange: japenese yen if please type yen // british sterling pound please type bsp // dollers please type usd // euro please type ero.")     if current_currency == "yen":         amount = input("type amount wish exchange")         future_currency = input("what currency want exchange into: japenese yen if please type yen // british sterling pound please type bsp // dollers please type usd // euro please type ero.")         new_amount = future_currency * amount 

i have build , apparently need float through research dont have clue on how implement it.

it looks confusing variable names variables. because currency type user string, can't used reference variables unless call eval on it.

new_amount = eval(future_currency) * amount 

the downside of using eval gives user possible way affect code. instead, can use dictionary. dictionaries map strings values, , can take variable declarations:

yen = 0.0067 bsp = 1.35 usd = 0.65 ero = 0.85 

and turn them dictionary:

currencies = {'yen': 0.0067, 'bsp': 1.35, 'usd': 0.65, 'ero': 0.85} 

using this, can find value looking in dictionary. don't forget handle incorrect user input properly!

currencies = {'yen': 0.0067, 'bsp': 1.35, 'usd': 0.65, 'ero': 0.85}  current_currency = raw_input() future_currency = raw_input() amount = int(raw_input()) // check errors in input here new_amount = amount * currencies[future_currency] / currencies[current_currency] 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

javascript - addthis share facebook and google+ url -

ios - Show keyboard with UITextField in the input accessory view -