Python 循环与字符统计示例代码
下面是两个符合你要求的 Python 代码示例:
- 使用 for-in 循环打印从 1 到 n 的数字:
n = int(input('请输入一个整数:'))
for i in range(1, n+1):
print(i, end=' ')
- 统计输入字符的个数:
space_count = 0
uppercase_count = 0
lowercase_count = 0
other_count = 0
while True:
char = input('请输入一个字符(输入'!'结束):')
if char == '!':
break
if char.isspace():
space_count += 1
elif char.isupper():
uppercase_count += 1
elif char.islower():
lowercase_count += 1
else:
other_count += 1
print('空格数量:', space_count)
print('大写字母数量:', uppercase_count)
print('小写字母数量:', lowercase_count)
print('其他字符数量:', other_count)
以上代码可以在 Python 环境中运行,按照要求输入相应的值即可得到结果。
原文地址: http://www.cveoy.top/t/topic/bFhz 著作权归作者所有。请勿转载和采集!