python - 如何以简单的方式区分输入中的数字?

标签 python python-3.x input types

我现在正在学习如何让我的程序区分一个函数中的两种类型的输入...我知道这是可能的,但我实际上该怎么做;-; 这是我的代码:

def alphanumeric_input_detection(base_input):
    if (type(base_input)==str):
        return (print("you've written it wrong you dumm dumm"))
    else:
        return(print("thats correct input"))

base_input=input("please write a number")

alphanumeric_input_detection(base_input)

最佳答案

您可以使用isnumeric()函数。如果字符串对象是数字,则返回 True。

s = '1'
print(s.isnumeric())
[Output] = True

关于python - 如何以简单的方式区分输入中的数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53134256/

相关文章:

python - 在配置单元中使用 python udf 时如何查询多列?

python - 为什么列表理解中的元组需要括号?

python-3.x - 计算特定暗区的平均宽度

python - 仅在 matplotlib 上移除底部错误上限

python - 当我将变量 'x' 更改为 'fab' 时发生了什么?有关装饰器并传递函数变量的信息

python - 如何使用 pandas 将行的开始日期和结束日期转换为每月 block ?

events - Chrome 输入 [type=number] 和 onchange

python - 在 Python Selenium chromedriver 中执行无限 JavaScript 循环时陷入困境

input - 运动对象未检测到任何碰撞 - Godot

c++ - 限制用户输入的字符数