【发布时间】:2018-10-21 05:53:51
【问题描述】:
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
新错误:
name 'balance' is used prior to global declaration
我要做的就是:在那里提取一些现金后更新当前余额!
【问题讨论】:
-
为什么不将
global balance移动到函数定义的顶部(而不是将其埋在else子句中)? -
你说的对,我是说看卡片功能,全局错误,我做对了,搞砸了服务功能 OMG xD
标签: python python-3.x