对您的代码进行了一些更改:
num=0
Stored_num=23
def input_num():
global num
num=int(input('Enter the number again: '))
while num!=Stored_num:
input_num()
else:
print('Congrats ! The entered number mathched the stored number')
结果:
Enter the number again: 10
Enter the number again: 5
Enter the number again: 23
Congrats ! The entered number mathched the stored number
Process finished with exit code 0
几点注意事项:
- Python 不共享全局范围变量。如果您需要修改在 a 之外定义的变量
def
,则需要使用global
- Python 是一种区分大小写的语言。(所以
Stored_num
不等于stored_num
)
有一个与此相关的错误num1
不存在。只是将其重命名为num
.
我想就是这样。(: