【发布时间】:2014-03-07 15:06:36
【问题描述】:
制作货币转换器但我有错误,首先打印
sorry not a valid currency
之后
Pound Sterling
Please enter the amount of money to convert: 100
['Pound Sterling', 'Euro', 'US Dollar', 'Japanese Yen']
Please enter the current currency: 'Euro'
1.22
Please enter the currency you would like to convert to: 'Pound Sterling'
Sorry, that is not a valid currency
81.9672131148
然后是局部变量错误
Traceback (most recent call last):
File "<pyshell#7>", line 1, in ?
converter()
File "//server01/ICT2124/Task 1/Currency Converter 2.py", line 19, in converter
exchange()
File "//server01/ICT2124/Task 1/Currency Converter 2.py", line 65, in exchange
newAmount = int(toPound)*float(newRt)
UnboundLocalError: local variable 'newRt' referenced before assignment
>>>
这里是代码,请帮助
def exchange():
crntAmnt = int(input("Please enter the amount of money to convert: "))
print(currencies)
exRtFile = open ('exchangeRate.csv')
exchReader = csv.reader(exRtFile)
crntCurrency = input("Please enter the current currency: ")
for row in exchReader:
currency = row[0]
if currency == crntCurrency:
crntRt = row[1]
print(crntRt)
break
else:
print("Sorry, that is not a valid currency")
newCurrency = input("Please enter the currency you would like to convert to: ")
for row in exchReader:
currency = row[0]
if currency == newCurrency:
newRt = row[1]
print(newRt)
break
else:
print("Sorry, that is not a valid currency")
toPound = crntAmnt/float(crntRt)
print(toPound)
newAmount = int(toPound)*float(newRt)
print("You have: " ,newAmount, newCurrency,)
return
【问题讨论】:
-
@Wooble:在这里没关系,不是吗?带有多个参数的
print()函数也不是线索? :-P -
@MartijnPieters:他在
input()的输入周围输入引号。这将在 Python 2 中生成正确的字符串(以错误的方式,授予...),例如"'Euro'"在 Python 3 中。
标签: python csv python-3.x