用Python解决下列问题 谢了
7-18统计字符(15分)本题要求编写程序,输入10个字符,统计其中英文字母、空格或回车、数字字符和其他字符的个数。输入格式:输入为10个字符。最后一个回车表示输入结束,...
7-18 统计字符(15 分)
本题要求编写程序,输入10个字符,统计其中英文字母、空格或回车、数字字符和其他字符的个数。
输入格式:
输入为10个字符。最后一个回车表示输入结束,不算在内。
输出格式:
在一行内按照
letter = 英文字母个数, blank = 空格或回车个数, digit = 数字字符个数, other = 其他字符个数
的格式输出。
输入样例:
aZ &
09 Az
输出样例:
letter = 4, blank = 3, digit = 2, other = 1 展开
本题要求编写程序,输入10个字符,统计其中英文字母、空格或回车、数字字符和其他字符的个数。
输入格式:
输入为10个字符。最后一个回车表示输入结束,不算在内。
输出格式:
在一行内按照
letter = 英文字母个数, blank = 空格或回车个数, digit = 数字字符个数, other = 其他字符个数
的格式输出。
输入样例:
aZ &
09 Az
输出样例:
letter = 4, blank = 3, digit = 2, other = 1 展开
2个回答
展开全部
废话不说,Python2、Python3上代码(Python3代码经过测试,Python2的环境损坏了,正准备重装)
Python3代码:
# Python 3 Code
letter = 0
blank = 0
digit = 0
other = 0
while True:
s = input()
if s != '':
blank = blank + 1
for i in s:
if '0' <= i and i <= '9':
digit = digit + 1
elif 'a' <= i and i <= 'z':
letter = letter + 1
elif 'A' <= i and i <= 'Z':
letter = letter + 1
elif i == ' ':
blank = blank + 1
else:
other = other + 1
else:
blank = blank - 1
break
print("letter = %s" % letter, end = ',')
print("blank = %s" % blank, end = ',')
print("digit = %s" % digit, end = ',')
print("other = %s" % other, end = ',')
# Finish Code
Python2代码:
# Python 2 Code
letter = 0
blank = 0
digit = 0
other = 0
while True:
s = raw_input()
if s <> '':
blank = blank + 1
for i in s:
if '0' <= i and i <= '9':
digit = digit + 1
elif 'a' <= i and i <= 'z':
letter = letter + 1
elif 'A' <= i and i <= 'Z':
letter = letter + 1
elif i == ' ':
blank = blank + 1
else:
other = other + 1
else:
blank = blank - 1
break
print "letter = %d,blank = %d,digit = %d,other = %d" % (letter, blank, digit, other)
# Finish Code
Python3代码:
# Python 3 Code
letter = 0
blank = 0
digit = 0
other = 0
while True:
s = input()
if s != '':
blank = blank + 1
for i in s:
if '0' <= i and i <= '9':
digit = digit + 1
elif 'a' <= i and i <= 'z':
letter = letter + 1
elif 'A' <= i and i <= 'Z':
letter = letter + 1
elif i == ' ':
blank = blank + 1
else:
other = other + 1
else:
blank = blank - 1
break
print("letter = %s" % letter, end = ',')
print("blank = %s" % blank, end = ',')
print("digit = %s" % digit, end = ',')
print("other = %s" % other, end = ',')
# Finish Code
Python2代码:
# Python 2 Code
letter = 0
blank = 0
digit = 0
other = 0
while True:
s = raw_input()
if s <> '':
blank = blank + 1
for i in s:
if '0' <= i and i <= '9':
digit = digit + 1
elif 'a' <= i and i <= 'z':
letter = letter + 1
elif 'A' <= i and i <= 'Z':
letter = letter + 1
elif i == ' ':
blank = blank + 1
else:
other = other + 1
else:
blank = blank - 1
break
print "letter = %d,blank = %d,digit = %d,other = %d" % (letter, blank, digit, other)
# Finish Code
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询