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 yen // British Sterling Pound please type bsp // US Dollers please Type usd // Euro please type ero.")
if Current_Currency == "yen":
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 = Future_Currency * amount
我必须建立这个,显然我需要通过研究浮动,但我不知道如何实现它。
答案 0 :(得分:2)
看起来你将变量名称与变量混淆了。由于您从用户获得的货币类型是一个字符串,因此除非您在其上调用eval,否则它不能用于引用变量。
new_amount = eval(future_currency) * amount
这样做的缺点是使用eval
为用户提供了影响代码的可能方法。相反,您可以使用字典。字典将字符串映射到值,因此您可以获取变量声明:
yen = 0.0067
bsp = 1.35
usd = 0.65
ero = 0.85
将它们变成字典:
currencies = {'yen': 0.0067, 'bsp': 1.35, 'usd': 0.65, 'ero': 0.85}
使用此功能,您可以在字典中找到要查找的值。不要忘记正确处理错误的用户输入!
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]
答案 1 :(得分:0)
该行
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.")
如果输入了未知变量,将抛出错误,并且Current_Currency
将被设置为用户提供的变量名称的值,因此该行
if Current_Currency == "yen":
并不是真的需要。
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