我有以下代码:使其可用的功能外变量
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'作为全局变量传递它。 –