我在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
不能检查它
为什么?在
这是因为
s
是string
类型的变量如果要检查字符串
s
中的每个符号是否为数字,则应尝试类似[x.isdigit() for x in a]
的方法。在因此,6将不会返回一个空的数字“6”。在
使用replace()函数可以去掉所有空格:
^{pr2}$相关问题 更多 >
编程相关推荐