【发布时间】:2020-06-01 13:57:10
【问题描述】:
我正在尝试添加一个例外来识别某人何时输入负数,并回复说您只能输入正数
print('How many cats do you have?')
numCats = input()
try:
if int(numCats) >=4:
print('Thats a lot of cats.')
else:
print('Thats not that many cats.')
except ValueError:
print('You did not enter a number.')
目前它会响应用户输入字符串而不是整数,但我希望它能够通过打印“您不能使用负数”来响应用户输入 -4 之类的内容。
对 Python 完全陌生,因此非常感谢任何有关如何添加它的建议,谢谢。
【问题讨论】:
-
您已经知道如何针对恒定阳性进行测试。那么负数有什么问题呢?
-
你可能想阅读python教程...docs.python.org/3/tutorial/errors.html#raising-exceptions