【发布时间】:2017-06-01 00:04:11
【问题描述】:
我有以下代码:
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)
现在我需要能够在主代码中的 while 之后读取变量“credit”,但我得到了错误
NameError: name 'credit' 未定义
如何从函数Payment 中提取变量credit 以在主程序中使用?
【问题讨论】:
-
您可以尝试使用
global credit将其作为全局变量传递。
标签: python python-3.x function variables