这是我班级的代码,我想使用 for 循环来计算某个字符的使用次数,我被告知要执行每个字符而不是一行,这是什么意思?除了基本的弦线之外,我大部分时间都没有头绪,我现在在线,大部分时间都无法理解老师的意思。这是我在字符计数方面遇到的唯一代码创建问题。
def main():
try:
uppercase = 0
lowercase = 0
digits = 0
whitespace = 0
string_input = open('text.txt', 'r')
character_strings = string_input.readlines()
for i in range(len(character_strings)):
character_strings[i] = character_strings[i].rstrip("\n")
uppercase = uppercase_counter(character_strings)
lowercase = lowercase_counter(character_strings)
digits = digit_counter(character_strings)
count1 = 0
count2 = 0
count3 = 0
for ch in string:
if ch.isupper():
count1 = count1 + 1
return count1
print(count1)
for ch in string:
if ch.islower():
count2 = count2 + 1
return count2
print(count2)
for ch in string:
if ch.isdigit():
count3 = count3 + 1
return count3
print(count3)
except IOError:
print("The file could not be found")
except IndexError:
print("There was an indexing error")
except:
print("An error occurred")
main()