繁体   English   中英

不能通过类型'str'的非int乘以序列不理解

[英]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 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

我必须建立这个,显然我需要通过研究浮动,但我不知道如何实现它。

看起来你将变量名称与变量混淆了。 因为从用户获得的货币类型是字符串,所以除非您在其上调用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]

这条线

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM