python - 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
解决方案
该行有错误: num = input("Pick a number: ")
因为输入法总是返回一个String
,所以你应该把它转换成 int 来执行integer
操作 正确的代码是:
num =int( input("Pick a number: "))
推荐阅读
- python - ubuntu中的文件路径有问题
- drupal - 未找到 Drupal 表单的页面
- sonarqube - 如何将 sonar.path.data 设置到不同的驱动器
- php - 如何在 API 平台中显示嵌套文档
- c# - .NET Core Dispose 实现
- python - 如何获取 Telegram 频道(超过 200 个)成员的列表
- python - 在 Python 中逐项列出大的 xml 文件
- javascript - snekfetch 请求中的点导致问题
- python-3.x - 对元组列表进行排序并在python中返回元组的第一个元素
- python - 关于 python 岭回归的问题:缩放和解释