非整数中断 整数输入

  • 本文关键字:整数 中断 python
  • 更新时间 :
  • 英文 :


当我在此代码中输入非数字时,它会中断以下消息:

回溯(最近一次调用(: 文件 "C:/Users/Default/Desktop/AS91076.py",第 12 行,在 temp = int(input("你想在什么温度下洗?(最大 40 度("(( ValueError:int(( 的文字无效,底数为 10:"asdf">

temp = int(input("At what Temperature do you want your wash? (Maximum 40 Degrees)"))
if temp < 41:
**Misc Code**
else:
print("Not a valid Temperature!")

任意字符串不能转换为数字。如果要捕获用户键入非整数的情况,可以使用try语句。

try:
temp = int(input("At what Temperature do you want your wash? (Maximum 40 Degrees)"))
if temp < 41:
**Misc Code**
else:
print("Temperature too high!")
except ValueError as e:
print("Not a valid Temperature!")

最新更新