|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
0.
>>> x = 1
>>> def q():
x = input('请输入:')
>>> q()
请输入:2
>>> x
1
为什么x不发生改变
1.
>>> def judge():
while True:
if int(num) != 11:
num = input('长度不对重新输入:')
else:
break
>>> num = 11
>>> judge()
Traceback (most recent call last):
File "<pyshell#342>", line 1, in <module>
judge()
File "<pyshell#340>", line 3, in judge
if int(num) != 11:
UnboundLocalError: local variable 'num' referenced before assignment
>>> num = 13
>>> judge()
Traceback (most recent call last):
File "<pyshell#344>", line 1, in <module>
judge()
File "<pyshell#340>", line 3, in judge
if int(num) != 11:
UnboundLocalError: local variable 'num' referenced before assignment
请问出错在哪?
感谢各位前辈
第一个问题:要声明全局变量才可以改变全局变量
def q():
global x
x = input('请输入:')
第二个问题:在赋值之前 num 并不存在,而你在赋值之前用到了 num 来判断,你应该给 num 赋初值
def judge():
num = 0
while True:
if int(num) != 11:
num = input('长度不对重新输入:')
else:
break
|
|