Python 使变量在函数外部可用
我有以下代码:Python 使变量在函数外部可用,python,python-3.x,function,variables,Python,Python 3.x,Function,Variables,我有以下代码: 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')
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)
def Payment():
打印(“成本为1”)
打印('请插入硬币')
信用=浮动(输入())
当信用<1时:
打印('您的信用现在是',信用,')
打印('您仍然需要添加其他',-credit+1,'cent')
newcredit=float(输入())
信用=新信用+信用
付款()
印刷(学分)
现在我需要能够在主代码中的while之后读取变量“credit”,但是我得到了错误
NameError:未定义名称“credit”
如何从函数
支付中提取变量credit
,以便在主程序中使用?将其作为函数结果返回:
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
return credit
balance = Payment()
print(balance)
def Payment():
打印(“成本为1”)
打印('请插入硬币')
信用=浮动(输入())
当信用<1时:
打印('您的信用现在是',信用,')
打印('您仍然需要添加其他',-credit+1,'cent')
newcredit=float(输入())
信用=新信用+信用
退货信用证
余额=付款()
打印(余额)
您应该返回@Prune等函数中的变量
但是,如果您确实希望将其作为变量,则必须在函数外部定义它,并在函数内部使用global credit
(这将告诉Python它应该在函数范围之外更改变量):
credit=0
def付款():
全球信贷
信用=浮动(输入())
当信用<1时:
newcredit=float(输入())
信用=新信用+信用
付款()
印刷(学分)
但是return
的替代方法要好得多,我只是介绍了它,因为它在评论中被提到过(两次)。除了返回信用之外,您还可以将该值存储到另一个变量中,并以这种方式进行处理。以防您需要进一步修改该信用变量。
new_变量=信用
打印(新变量)
这比您想象的要容易。
首先,分配一个名为credit的变量(在函数外)。这将不会与任何函数交互
credit = 0
让您的函数除了添加一个参数和一个return语句之外,都可以使用
def Payment(currentcredit):
...
return currentcredit - credit #lose credit after payment
最后,
credit = Payment(credit)
最后的代码是
credit = 100
def Payment(currentcredit):
print('The cost for''is 1£')
print('Please insert a coin')
credit = float(input())
while credit < 1:
print('Your credit is now', currentcredit,'£')
print('You still need to add another',-credit+1,'cents!')
newcredit = float(input())
credit = newcredit + credit
return currentcredit - credit
credit = Payment(credit)
print(credit)
credit=100
def付款(当前信用证):
打印(“成本为1”)
打印('请插入硬币')
信用=浮动(输入())
当信用<1时:
打印('您的信用现在是',当前信用,')
打印('您仍然需要添加另一个',-credit+1,'cents!')
newcredit=float(输入())
信用=新信用+信用
返回当前信用-信用
信用=付款(信用)
印刷(学分)
输出
CON:“”的成本为1;
CON:请插入硬币
ME:0.5
CON:您的信用现在是100英镑
CON:您仍然需要再添加0.5美分
ME:49.5
变量“信用”更新为付款(100)=>50
CON:50
[50学分=100学分减去50学分损失]
工作起来很有魅力。你也可以将信用声明为一个全局变量,尽管这不是一个真正的好风格,我很清楚这一点——它是可能的,也是一个糟糕的风格。教他们走哪条路…:-)您可以尝试使用global credit
将其作为全局变量传递。
credit = 100
def Payment(currentcredit):
print('The cost for''is 1£')
print('Please insert a coin')
credit = float(input())
while credit < 1:
print('Your credit is now', currentcredit,'£')
print('You still need to add another',-credit+1,'cents!')
newcredit = float(input())
credit = newcredit + credit
return currentcredit - credit
credit = Payment(credit)
print(credit)