我有以下代碼:使其可用的功能外變量
def Payment():
print('The cost for''is 1£')
print('Please insert coin')
credit = float(input())
while credit < 1:
print('Your credit now is', credit,'£')
print('You still need to add other',-credit+1,'cent')
newcredit = float(input())
credit = newcredit + credit
Payment()
print(credit)
現在我需要的是能夠同時在主代碼後讀變量「信用」,但我得到的錯誤
NameError: name 'credit' is not defined
如何從函數Payment
中提取變量credit
以在主程序中使用?
您可以嘗試使用'global credit'作爲全局變量傳遞它。 –