首页 > 解决方案 > How do I check if a input is an integer type

问题描述

I tried to make a odd/even 'calculator' in python and it keeps popping up errors. Here's the code:

def odd_even():
    print("Welcome to Odd/Even")
    num = input("Pick a number: ")
    num2 = num/2
    if num2 == int:
        print("This number is even")
    else:
        print("This number is odd")

Id like to know whats causing the errors and solutions to them

标签: pythonpython-3.xstring

解决方案


该行有错误: num = input("Pick a number: ") 因为输入法总是返回一个String,所以你应该把它转换成 int 来执行integer操作 正确的代码是:

num =int( input("Pick a number: "))

推荐阅读