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

2024-06-08 22:35:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我在python上编写小程序只是为了学习split和{}函数是如何工作的

课程是:

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不能检查它

为什么?在


Tags: to程序youisit数字integerresult
2条回答

这是因为sstring类型的变量

如果要检查字符串s中的每个符号是否为数字,则应尝试类似[x.isdigit() for x in a]的方法。在

因此,6将不会返回一个空的数字“6”。在

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

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

^{pr2}$

相关问题 更多 >