【问题标题】:UnboundLocalError: local variable referenced before assignment using csv fileUnboundLocalError:使用 csv 文件分配之前引用的局部变量
【发布时间】: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


【解决方案1】:

newRtexchReader 中有行时设置并且if currency == newCurrencyTrue 的一行。

由于没有匹配的货币,其余代码无法合理运行,因此只需在该点返回:

else:
    print("Sorry, that is not a valid currency")
    return

exchReader 中没有行的原因是您无法循环遍历 CSV 阅读器两次;您最好将文件中的所有数据存储在字典中:

with open('exchangeRate.csv') as exRtFile:
    exchReader = csv.reader(exRtFile)
    currencies = {row[0]: float(row[1]) for row in extchReader}

crntCurrency = input("Please enter the current currency: ")
if crntCurrency not in currencies:
    print("Sorry, that is not a valid currency")
    return

newRt = currencies[newCurrency]

toPound = crntAmnt / crntRt
print(toPound)
newAmount = int(toPound) * newRt
print("You have: ", newAmount, newCurrency)

【讨论】:

  • 不,因为那样我就不能更改保存更改,稍后会出现
  • @user3165683: 你有一个return 声明稍后无论如何
  • @eryksun:我通过创建字典来解决整个问题。
  • @eryksun:在这种情况下,另一种选择是先询问两种货币,然后扫描 CSV 一次 找到两者。
  • @eryksun:如果文件那么大,你真的不想循环两次。
【解决方案2】:

在表达式的情况下:

if currency == newCurrency

将试图在 无条件(即是否每次都执行)语句:

newAmount = int(toPound)*float(newRt)

未初始化,因此 将引发错误

(编辑)正如 Martin 建议的那样,您应该在以下位置添加一个 return 语句:

print("Sorry, that is not a valid currency")

这意味着它现在应该是这样的:

print("Sorry, that is not a valid currency")
return

【讨论】:

  • 我是初学者,虽然我在前面的'for'语句中初始化了,但我不明白
  • 对,但让我们这样说吧:变量 newRt 仅在货币 == newCurrency 的情况下“获取”一个值。如果货币 == newCurrency 为 False,那么 newRt 将没有任何值,当您稍后尝试访问它时,它会抛出错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-19
  • 2017-08-10
相关资源
最近更新 更多