Python 名称';余额';在全局声明之前使用
我无法纠正这个错误:Python 名称';余额';在全局声明之前使用,python,python-3.x,Python,Python 3.x,我无法纠正这个错误: pin = input("Set your PIN: ") balance = 1000 wrong = 1 def service(): nextt = int(input("\nWelcome! \n Press 1 - to withdraw some money and \n Press 2 - to exit the service \n Press 3 - to show
pin = input("Set your PIN: ")
balance = 1000
wrong = 1
def service():
nextt = int(input("\nWelcome! \n Press 1 - to withdraw some money and \n
Press 2 - to exit the service \n Press 3 - to show
current balance \n "))
if nextt == 1:
amount = int(input("\nHow much money do you want: "))
if amount > balance:
print("Sorry, but your current balance is only: " +str(balance)+" $")
service()
else:
print("You withdraw: " +str(amount) +" from your account and your
current balance is: " +str(balance-amount)+" $")
global balance
balance -= amount
service()
elif nextt == 2:
print("Thank you for your visit // exiting...")
return
elif nextt == 3:
print("Your current balance is: " +str(balance)+" $")
service()
else:
print("\n Failure: Press 1 to withdraw some money, 2 to exit the service
and 3 to show current balance!")
service()
def card():
global wrong
choose = input("\nEnter your card PIN: ")
if choose == pin:
service()
else:
print("\n You entered the wrong PIN!")
wrong = wrong + 1
if wrong > 3:
print("\n You reached the max. amount to enter your PIN correctly,
exiting...")
return
else:
card()
card()
我想在提取一些现金后更新余额,但上面写着:
on line 14: balance -= amount
我补充说
local variable 'balance' referenced before assignment
新错误:
global balance
balance -= amount
我想做的就是:在那里提取一些现金后更新当前余额 在上面的几行中,您隐式地通知Python,
balance
是一个局部变量:
name 'balance' is used prior to global declaration
以前在这个名称空间(函数范围)中没有见过它,所以它是一个局部变量。当你进入else
子句时,你突然让Python跳了一段“我撒谎”的舞蹈,宣称它是全球性的。巨蟒对这种滑稽动作不感兴趣
如果希望
balance
成为一个全局变量(错误做法),则按照编码指南中的建议,在块的顶部声明它。更好的方法是,将其作为函数参数传入,并在完成后返回。有时可以方便地准确列出正在运行的代码:
if amount > balance:
要解决这个问题,可以使用global
关键字,在全局范围内查找变量。在找到解决方案之前,让我们看一个例子,说明这是如何工作的:
nextt = int(input("\nWelcome! ...")) # "nextt" is being set in the local scope
if nextt == 1: # "nextt" is being compared to 1
amount = int(input("\nHow ...")) # "amount" is being set in the local scope
if amount > balance: # "amount" is being compared to "balance"
# Uh oh! We haven't defined "balance" yet!
print("Sorry, ...")
...
def f1():
a = 2
print("Value of a: {}; ID of a: {}".format(a, id(a)))
def f2():
global a
print("[Before] Value of a: {}; ID of a: {}".format(a, id(a)))
a = 3
print("[After] Value of a: {}; ID of a: {}".format(a, id(a)))
>>> a = 1 # we define "a" in the global scope
>>> a, id(a) # id(variable) gets a unique value for this variable
(1, 1635934432)
>>> f1()
Value of a: 2; ID of a: 1635934464 # So you see that the ID is different, because this is f1's version of "a", not the global one
>>> a, id(a)
(1, 1635934432) # the value and ID of the global variable remain unchanged
>>> f2()
[Before] Value of a: 1; ID of a: 1635934432 # see the value and ID of the local "a" are now the same as the global variable
[After] Value of a: 3; ID of a: 1635934496 # we replaced the value and ID here
>>> a, id(a)
(3, 1635934496) # which has affected the global version now as well
最后,完整的解决方案:
nextt = int(input("\nWelcome! ...")) # "nextt" is being set in the local scope
if nextt == 1: # "nextt" is being compared to 1
amount = int(input("\nHow ...")) # "amount" is being set in the local scope
if amount > balance: # "amount" is being compared to "balance"
# Uh oh! We haven't defined "balance" yet!
print("Sorry, ...")
...
def f1():
a = 2
print("Value of a: {}; ID of a: {}".format(a, id(a)))
def f2():
global a
print("[Before] Value of a: {}; ID of a: {}".format(a, id(a)))
a = 3
print("[After] Value of a: {}; ID of a: {}".format(a, id(a)))
>>> a = 1 # we define "a" in the global scope
>>> a, id(a) # id(variable) gets a unique value for this variable
(1, 1635934432)
>>> f1()
Value of a: 2; ID of a: 1635934464 # So you see that the ID is different, because this is f1's version of "a", not the global one
>>> a, id(a)
(1, 1635934432) # the value and ID of the global variable remain unchanged
>>> f2()
[Before] Value of a: 1; ID of a: 1635934432 # see the value and ID of the local "a" are now the same as the global variable
[After] Value of a: 3; ID of a: 1635934496 # we replaced the value and ID here
>>> a, id(a)
(3, 1635934496) # which has affected the global version now as well
简单的修复方法(如果您坚持使用全局变量)是将全局语句重新定位到def service
函数中的第一条语句:
global balance # "balance" is set to the global value
nextt = int(input("\nWelcome! ...")) # "nextt" is being set
if nextt == 1: # "nextt" is being compared to 1
amount = int(input("\nHow much ...: ")) # "amount" is being set
if amount > balance: # "amount" is being compared to "balance"
print("Sorry, ...")
...
您的代码非常自由地使用globals,这是一个坏主意。有人提出了其他建议来纠正这一点,因此我不再进一步详述:)为什么不将
全局平衡
移到函数定义的顶部(而不是将其埋在else
子句中)?你完全正确,我的意思是看一下卡函数,全局错误,我做得对,把服务功能OMG xD搞砸了