如何测试变量是否为负数?

时间:2018-10-07 21:35:50

标签: python python-3.x

第一篇文章,反馈表示赞赏!

我是python的新手,当我遇到有关错误处理的练习时,目前正在通过“自动化无聊的东西”进行工作。 在练习中,我必须考虑到在对 input()的响应中没有键入 int 值的人,这是 try: -> 例外:非常有用。但是,如果我想在插入 negativ int 值时打印一条消息怎么办?

下面的我的代码显示 except 行。

print('How many cats do you have')
numCats = input()

try:
    if int(numCats) >= 4:
        print('That is a lot of cats')
    elif numCats < 0:
        print('Did you give cats away you did not own?')
    else:
        print('That is not taht many cats')
except:
    print('You did not enter a number.')

0 个答案:

没有答案
相关问题