【发布时间】:2022-01-07 08:06:57
【问题描述】:
如果我输入除 1 以外的任何值/字符,如何让它退出
【问题讨论】:
-
您可以随时使用
else声明exit! -
如果在 int() 转换尝试期间由于 ValueError 异常而输入整数以外的任何内容,它将自行“中断”
标签: python if-statement while-loop integer
如果我输入除 1 以外的任何值/字符,如何让它退出
【问题讨论】:
else 声明exit!
标签: python if-statement while-loop integer
import sys
while True:
i = input('Please enter confirm (input 1) to proceed with
username and password creation (or any to quit):\n')
if i.isnumeric() == 1:
print("Generating username and password....")
break
else:
sys.exit()
【讨论】:
i 的值为any 数字,您的if 语句if i.isnumeric() == 1: 就会运行!因为,当您编写i.isnumeric() == 1 时,它会将1 评估为True。因此,即使我输入2或1234567890,也会显示Generating username and password....。