统计数字字符个数

所属作业: hw4 算法: 循环

代码1

1
2
3
4
5
6
cnt = 0
s = input()
for x in s:
	if '0' <= x <= '9':
		cnt += 1
print(cnt)

代码2

1
2
3
4
5
6
s=input()
count=0
for char in s:
    if char.isdigit():
        count+=1
    print(count)

找bug:

1
2
3
4
5
6
7
8
word = input()
sum = 0
for letter in word:
    for i in range(9):
        i = str(i)
        if letter == i:
            sum += 1
print(sum)

判断数字的方法

1
2
3
4
'0' <= x <= '9' 
'0' <= x and x <= '9' 
x.isdigit() 
x in '0123456789'

错误写法

1
2
3
4
# 错的
s = input()
for x in s:
  if x == '0' or '1' or '2' ...