【发布时间】:2021-10-18 21:23:05
【问题描述】:
我需要python检查一个变量是否为整数,然后采取相应的行动。
下面是相关代码:
def data_grab():
global fl_count #forklift count
print("How many Heavy-lift forklifts can you replace?\n\n"
"Please note: Only forklifts that have greater than 8,000 lbs. of lift capacity will qualify for this incentive.\n"
"**Forklifts do NOT need to be located at a port or airport to be eligible.**")
forklift_count = input("Enter in # of forklifts:")
if type(forklift_count) is int:
fl_count = forklift_count
else:
print("Invalid number. Please try again.")
data_grab()
目前,当用户实际输入整数时,会自动跳转到ELSE,而不是执行IF下的代码。
有什么想法吗?
【问题讨论】:
-
库参考是检查您正在使用的函数的好地方。你可以从input docs 看到它总是返回一个字符串。你的类型检查总是会失败。
标签: python if-statement input integer