【问题标题】:How do i input any number/alphabet/character in python我如何在python中输入任何数字/字母/字符
【发布时间】:2022-01-07 08:06:57
【问题描述】:

如果我输入除 1 以外的任何值/字符,如何让它退出

【问题讨论】:

  • 您可以随时使用else 声明exit
  • 如果在 int() 转换尝试期间由于 ValueError 异常而输入整数以外的任何内容,它将自行“中断”

标签: python if-statement while-loop integer


【解决方案1】:
   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()

【讨论】:

  • 问题是专门检查值 1。如果输入是任何数字字符串,此代码将执行 print()
  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
  • 实际上,如果i 的值为any 数字,您的if 语句if i.isnumeric() == 1: 就会运行!因为,当您编写i.isnumeric() == 1 时,它会将1 评估为True。因此,即使我输入21234567890,也会显示Generating username and password....
猜你喜欢
  • 2022-11-25
  • 2020-05-15
  • 2020-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-24
  • 2014-12-30
  • 1970-01-01
相关资源
最近更新 更多