【发布时间】:2021-07-02 11:05:49
【问题描述】:
我希望在输入数据为 5 时打破内部 while 循环,但如果输入不是 5,则会再次执行循环。在这段代码中,我输入的每个数字的输入值都打印为 X = 1:
x = 1
while True:
def state():
x = input("Enter Your number:")
while True:
print(x)
if x==5:
print('x=5')
break
state()
在这段代码中,我试图解决这个问题,但也没有成功。
condition = True
x = 1
while True:
def state():
x = input("Enter Your number:")
while condition:
print(x)
if x==5:
print('x=5')
condition = False
state()
有人帮我吗?
【问题讨论】:
-
这里有多个问题。 1) state() 中
x的范围仅限于该函数(最好使用返回值或使用global关键字)。 2)input返回一个字符串,"5"永远不会等于数字5。
标签: python python-3.x while-loop