我正在编写一个脚本,可以在多个点接收用户输入。如果输入正确,我所需要的就是跳出循环并继续。但如果它是错误的,我想要做的是删除然后重新提示输入。这是我正在努力的一般情况:while True: test = input("Enter test to check: ") if test == expected: break else: print("Error: test is incorrect") #Clear/overwrite test #Re-prompt for input我一直在做一些研究,但我没有发现任何对我来说真正有意义的东西。有没有办法删除或覆盖用户输入的值,以便再次输入测试时,它是一个全新的值?
2 回答

潇湘沐
TA贡献1816条经验 获得超6个赞
如果在再次询问用户输入之前清除屏幕对您来说非常重要,您可以在显示错误消息后等待一段时间,然后再次询问用户输入以消除错误消息。
import time
expected = 'foo'
while True:
print(' '*30 + '\r', end='')
test = input("Enter test to check: ")
if test == expected:
break
else:
print("Error: {} is incorrect\r".format(test), end='')
time.sleep(2)
如果您想清除错误消息和以前的输入,您可以使用例如os.system('clear').

喵喵时光机
TA贡献1846条经验 获得超7个赞
为什么不做这样的事情
lol=True
while lol:
test = input("Enter test to check: ")
if test == expected:
lol=False
print("Corect!!!")
else:
print("Error: test is incorrect")
希望能帮助到你
添加回答
举报
0/150
提交
取消