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

前端 未结 2 1807
我在风中等你
我在风中等你 2021-01-28 18:14
yen = 0.0067
bsp = 1.35
usd = 0.65
ero = 0.85

if choice == \"2\":
    Current_Currency = input(\"What currency do you want to exchange: Japenese Yen if so please type y         


        
相关标签:
2条回答
  • 2021-01-28 19:05

    It looks like you are confusing variable names with variables. Because the currency type you get from the user is a string, it can't be used to reference variables unless you call eval on it.

    new_amount = eval(future_currency) * amount
    

    The downside of this is that using eval gives the user a possible way to affect your code. Instead, you can use a dictionary. Dictionaries map strings to values, and so you can take your variable declarations:

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

    And turn them into a dictionary:

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

    Using this, you can find the value you are looking for in the dictionary. Don't forget to 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 for errors in input here
    new_amount = amount * currencies[future_currency] / currencies[current_currency]
    
    0 讨论(0)
  • 2021-01-28 19:05

    The line

    Current_Currency = input("What currency do you want to exchange: Japenese Yen if so please type yen // British Sterling Pound please type bsp // US Dollers please Type usd // Euro please type ero.")
    

    will throw an error if an unknown variable is entered, and Current_Currency will be set to the value of the variable name provided by the user, so the line

    if Current_Currency == "yen":
    

    isn't really needed.

    yen = 0.0067
    bsp = 1.35
    usd = 0.65
    ero = 0.85
    
    Current_Currency = input("What currency do you want to exchange: Japenese Yen if so please type yen // British Sterling Pound please type bsp // US Dollers please Type usd // Euro please type ero.")
    amount = input("Type amount you wish to exchange")
    Future_Currency = input("What currency do you want to exchange into: Japenese Yen if so please type yen // British Sterling Pound please type bsp // US Dollers please Type usd // Euro please type ero.")
    New_Amount =  Current_Currency / Future_Currency * amount
    
    0 讨论(0)
提交回复
热议问题