简体   繁体   English

python3 如果没有用户输入更新变量,我如何继续循环?

[英]python3 How do I continue a loop if no user input to update variable?

I would like a while loop to continue and only read the updated variable 'if' user adds one.我想要一个while循环继续并且只读取更新的变量'if'用户添加一个。

value = 2
print("Enter new/updated value: ")
while True:
   if():
      value = input()
   print(value)

output should be: output 应该是:

Enter new/updated value: 
2
2
2
2...

until I enter a new value '3' into the keyboard and then press enter (does not stop)直到我在键盘中输入一个新值 '3' 然后按回车键(不会停止)

3
3
3
3...

or if I press 4 'and then enter' on the keyboards, I should get或者如果我在键盘上按 4 '然后输入',我应该得到

4
4
4
4...

How update a variable similar to a chat session?如何更新一个类似于聊天session的变量? Waiting for user input but without stopping the loop.等待用户输入但不停止循环。

all I get are 2's and an updated value is never accepted.我得到的只是 2,并且永远不会接受更新的值。

you can add a string in input() function to write a message.您可以在input() function 中添加一个字符串来写一条消息。 A break statment allow us exit from a loop and you need to validate your input before Try this code:D中断语句允许我们退出循环,您需要在尝试此代码之前验证您的输入:D

value = 2
while True:
  value = input("Enter new/updated value: ")
  if(value == 3):
    break;    
  else :
    print(value)

It seems you are looking for something like multi-threading and event listeners , which is not as easy or beginner-friendly as you might expect... I think you should check out tutorials with those key words - or question if you really need functionality like that right now.似乎您正在寻找诸如多线程事件侦听器之类的东西,这并不像您期望的那样容易或对初学者友好...我认为您应该查看带有这些关键字的教程-或者询问您是否真的需要功能像现在这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM