【发布时间】:2016-11-08 20:44:31
【问题描述】:
我需要一个程序,要求用户输入任何文本,然后显示三个字符串,第一个由文本中的所有元音组成,第二个由所有辅音组成,第三个由所有其他字符组成。我现在在一个while循环中,我想知道如何将它转移到Python中的for循环中。
text = input("Enter text: ")
# Loop counter
i = 0
# Accumulators
vows_string = ""
cons_string = ""
other_str = ""
while i < len(text):
char = text[i]
if char in "aioueAIOUE":
vows_string += char
elif char.isalpha():
cons_string += char
else:
other_str += char
i += 1
# Add pseudo-guillemets to make spaces "visible"
print(">>" + vows_string + "<<")
print(">>" + cons_string + "<<")
print(">>" + other_str + "<<")
【问题讨论】:
标签: python parsing for-loop while-loop