[英]How are the letters deleted from this "Vowel eater"?
word = input("enter a word: ")
word = word.upper()
for letter in word:
if letter == "A":
continue
elif letter == "E":
continue
elif letter == "I":
continue
elif letter =="O":
continue
elif letter == "U":
continue
else:
print(letter)
If I used Joseph as an example, it would return JSPH but I have no idea how the vowels are "deleted"如果我以 Joseph 为例,它会返回 JSPH,但我不知道元音是如何“删除”的
The letter variable takes one character from the input and compares using the if-else statement. letter 变量从输入中获取一个字符并使用 if-else 语句进行比较。 If the character matches the vowels the letter is not printed.如果字符与元音匹配,则不打印字母。
That means the program is only printing the non-vowel characters.这意味着程序只打印非元音字符。
For continue
, it essentially skips to the end of the loop and starts the next loop, so following through your loop:对于continue
,它本质上会跳到循环的末尾并开始下一个循环,因此请执行循环:
continue
.如果当前字母是元音,则continue
。 (This skips to the next letter in the loop, so the line print(letter)
will not be run). (这会跳到循环中的下一个字母,因此不会运行print(letter)
行)。else
statement and prints the letter.如果当前字母不是元音,则到达else
语句并打印该字母。 This means that in the end, the program only prints letters which are not vowels, as whenever a vowel is reached continue
is run meaning it skips to the next letter (so the letter is not printed).这意味着最后,程序只打印不是元音的字母,因为只要到达一个元音, continue
就会运行,这意味着它会跳到下一个字母(因此不会打印该字母)。
Side note: even if you didn't use continue
in each elif
statement, and used maybe pass
instead (which is just a "blank" instruction), the code would still work as by entering one of the if
or elif
options in the if
statement, it means that it won't run any of the other elif
or else
s afterwards, so the print(letter)
wouldn't be called either way.边注:即使你不使用continue
在各elif
语句,可以用于pass
输入的一个,而不是(这仅仅是一个“空白”指令),代码仍然工作作为if
或elif
的选项if
语句,这意味着它之后不会运行任何其他elif
或else
s,因此不会以任何方式调用print(letter)
。 A better way to show the use of continue
would be to place the print(letter)
outside and after the if
statement.显示continue
使用的更好方法是将print(letter)
放在if
语句的外部和之后。
Use regex使用正则表达式
word = input("enter a word: ")
word = word.upper()
import re
re.sub("[AEIOU]","", word)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.