Python 如何检查某个变量输入是否为特定类型

Python 如何检查某个变量输入是否为特定类型,python,Python,我一直在尝试使我的简单代码工作,并搜索不同的检查方法,以查看在使用if语句时某个变量是否为特定类型 # Are you tall enough to ride the roller coaster print('This will determine if you are able to ride this Rollar Coaster?') age = int(input('How old are you? ')) if isinstance(age, int): print('Pl

我一直在尝试使我的简单代码工作,并搜索不同的检查方法,以查看在使用if语句时某个变量是否为特定类型

# Are you tall enough to ride the roller coaster

print('This will determine if you are able to ride this Rollar Coaster?')
age = int(input('How old are you? '))
if isinstance(age, int):
    print('Please input a valid age')
elif age <= 12:
    print('You are not old enough to ride this ride!')
else:
    height = int(input('How tall are you? Enter in centimeters please: '))
    if height > 72:
        print('You may enter, enjoy the ride')
    else:
        print('You are not tall enough to ride.')
#你够高可以坐过山车吗
打印('这将决定您是否能够乘坐过山车?')
年龄=int(输入('你多大?'))
如果isinstance(年龄,整数):
打印('请输入有效年龄')
elif 72岁:
打印('您可以进入,享受旅程')
其他:
打印('你不够高,不能骑车')

我搜索了stack overflow并用谷歌搜索了一下,发现了isinstance和issubclass,但它们似乎不起作用。我还尝试了
while!=int
虽然我不能完全确定代码是否有效。

您可以使用try-except检查错误如果try-code有错误,那么执行except,这与if-else几乎相同

print('This will determine if you are able to ride this Rollar Coaster?')
try:
    age = int(input('How old are you? '))
    if age <= 12:
        print('You are not old enough to ride this ride!')
    else:
        height = int(input('How tall are you? Enter in centimeters please: '))
        if height > 72:
            print('You may enter, enjoy the ride')
        else:
            print('You are not tall enough to ride.')

except:
    print('Please input a valid age')
print('这将决定您是否能够乘坐过山车?')
尝试:
年龄=int(输入('你多大?'))
如果72岁:
打印('您可以进入,享受旅程')
其他:
打印('你不够高,不能骑车')
除:
打印('请输入有效年龄')

更正的代码:

# Are you tall enough to ride the roller coaster

print('This will determine if you are able to ride this Rollar Coaster?')
try:
    age = int(input('How old are you? '))
    if age <= 12:
        print('You are not old enough to ride this ride!')
    else:
        height = int(input('How tall are you? Enter in centimeters please: '))
        if height > 72:
            print('You may enter, enjoy the ride')
        else:
            print('You are not tall enough to ride.')
except ValueError:
    print("Please enter valid age.")
except Exception as e:
    print(e) # Other exceptions
#你够高可以坐过山车吗
打印('这将决定您是否能够乘坐过山车?')
尝试:
年龄=int(输入('你多大?'))
如果72岁:
打印('您可以进入,享受旅程')
其他:
打印('你不够高,不能骑车')
除值错误外:
打印(“请输入有效年龄”)
例外情况除外,如e:
打印(e)#其他例外情况
说明: 在代码中
age=int(输入('你几岁?)
在这里,
input
函数总是返回一个字符串,并使用
int()
将字符串输入转换为整数。所以,现在年龄是一个整数。因此,以下if条件将始终返回
true

if isinstance(age, int):
    print('Please input a valid age')

因此,即使您的程序收到有效的输入,它也会要求有效的输入。

如果输入错误,则已经有错误。
ValueError:int()的无效文本以10为基数:(您的输入)
这将为您指定的
age
行触发。您当前的检查是多余的,年龄在上面一行中被强制为整数。您应该利用该行引发的异常(请参阅Shadowcoder的注释),并在除blockSee和。你看看怎么做。这个问题在Stack Overflow的几个地方都有答案,在Internet上也有很多答案。谢谢。如果我想再问一次这个问题呢?在上面写着“请输入有效年龄”之后,代码就在这里断开了。@godfirst在页面顶部看到链接的副本page@Godfist您可以使用“while”循环,并一直迭代。如果用户提供了正确的数据,那么您可以简单地使用“break”跳出循环。明白了,谢谢您的帮助不要试图捕捉异常,为什么?您的try-except应该尽量少包装代码不要使用bare-except子句,try-except应该包装最少的代码
if isinstance(age, int):
    print('Please input a valid age')