Python 请给我澄清一下
尽管输入了存储值(即23),但上述代码不接受else条件,也不打印消息Python 请给我澄清一下,python,printing,output,func,alloca,Python,Printing,Output,Func,Alloca,尽管输入了存储值(即23),但上述代码不接受else条件,也不打印消息 请帮助我理解。您已将变量命名为Stored_num,并使用Stored_num进行比较。Python是一种区分大小写的语言,因此它们是不同的。将其中一个变量更改为另一个变量,应该可以正常工作。您没有键入全局变量和函数范围变量 num=0 存储数量=23 def input_num(): num1=int(输入('再次输入数字:')) 请参阅,var Stored_num=23以大写字母S开头,内部Stored_num以小字母
请帮助我理解。您已将变量命名为
Stored_num
,并使用Stored_num
进行比较。Python是一种区分大小写的语言,因此它们是不同的。将其中一个变量更改为另一个变量,应该可以正常工作。您没有键入全局变量和函数范围变量
num=0
存储数量=23
def input_num():
num1=int(输入('再次输入数字:'))
请参阅,var Stored_num=23以大写字母S开头,内部Stored_num以小字母S开头。
使两个变量名相同,它就会工作。对代码做了一些更改:
while num1!=stored_num:
input_num()
else:
print('Congrats ! The entered number mathched the stored number')
结果:
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')
几点注意:
def
之外的变量,则需要使用global
storaged_num
不等于storaged_num
)num1
相关的错误不存在。刚刚将其重命名为num
我想就是这样。(:
while不需要其他。事实上,这是一个错误的语法。
不,这是有效的语法,就像for else
一样。检查此代码段:哦,我不知道。将更新答案。感谢您指出这一点!(:@Rikki,非常感谢,先生。注1关于全局范围变量的内容从您的详细回答中非常有见地。我之前不知道。再次非常感谢。@mushahidq,谢谢您,先生。我也尝试了同样的方法,但是如果没有全局范围变量的函数,输出结果并不像预期的那样。非常感谢您的回答回答。@Rahul Maurya,谢谢你,先生。我也尝试了同样的方法,但是如果函数中没有全局范围变量,输出结果就不符合预期。非常感谢你的回答。
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