繁体   English   中英

Python。 是数字应该检查多个数字

[英]Python. Is digit should check for multiple number

我在 python 上编写小程序只是为了学习splitisdigit函数的工作原理

程序是:

s = input('type something:')
if s.isdigit():
    a = s.split()
    a = list(map(int, a))
    print('What you typed was number and it was converted to integer')
    print('Result is:', a)
else:
    a = s.split()
    print('What you typed was words it was not converted to integer')
    print('Result is:', a)

问题是什么...当我输入一个单一的数字程序工作正常。 isdigit检查号码。 (真的是列表包含的数字)。

当我输入 4 时(只有一个数字 - 没关系)

但是当我输入 3 6 4 2 6 3 多个数字时isdigit无法检查

为什么?

正如所有评论已经说过的那样:空白不是数字,因此字符串“3 6 4 2 6 3”将返回 False。

>>> print("3 6 4 2 6 3".isdigit())
False

您可以使用 replace() 函数去除所有空格:

>>> print("3 6 4 2 6 3".replace(" ", "").isdigit())
True

这是因为sstring类型的变量

如果您想检查字符串s每个符号是否为数字,您应该尝试类似[x.isdigit() for x in a]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM