编写一个程序,该程序接受句子作为控制台输入并计算上大写字母的数量,较低的案例字母和其他字符。
假设以下输入提供给程序:你好世界;!#
由于这个问题听起来像是编程作业,所以我写了这是一种更奇怪的方式。这是标准的Python 3,而不是Jes。
#! /usr/bin/env python3
import sys
upper_case_chars = 0
lower_case_chars = 0
total_chars = 0
found_eof = False
# Read character after character from stdin, processing it in turn
# Stop if an error is encountered, or End-Of-File happens.
while (not found_eof):
try:
letter = str(sys.stdin.read(1))
except:
# handle any I/O error somewhat cleanly
break
if (letter != ''):
total_chars += 1
if (letter >= 'A' and letter <= 'Z'):
upper_case_chars += 1
elif (letter >= 'a' and letter <= 'z'):
lower_case_chars += 1
else:
found_eof = True
# write the results to the console
print("Upper-case Letters: %3u" % (upper_case_chars))
print("Lower-case Letters: %3u" % (lower_case_chars))
print("Other Letters: %3u" % (total_chars - (upper_case_chars + lower_case_chars)))
请注意,您应该修改代码以自己处理线结束字符。目前,它们被视为"其他"。我也没有处理二进制输入的处理,可能 str()将失败。