我打算在“!”之后停止阅读输入。出现。这是我的代码的一部分:
msg = input("What is your message? ")
print()
lett = 0
otherch = 0
num_vowels = 0
num_consanants = 0
count_character = [0] * 100000
for character in msg.upper():
if character == "!":
print("lett =", lett)
print("other char = ", otherch)
print("num_vowels = ", num_vowels)
print("num_consanants = ", num_consanants)
elif character < "A" or character > "Z":
otherch = otherch + 1
count_character[ord(character)] = count_character[ord(character)] + 1
else:
lett = lett + 1
count_character[ord(character)] = count_character[ord(character)] + 1
for character in msg.upper():
print("character", character, "appeared" , count_character[ord(character)] , "times")
但是,如果我运行程序并输入诸如“hello!hi”之类的内容,它会继续读取感叹号之后的文本并继续说输入中有两个H和一个I. 有帮助吗?
答案 0 :(得分:1)
在break
块内使用if character == "!":
。它跳出for character in msg.upper():
循环。