我目前正试图找到一种方法将这个 for 循环更改为一个 while 循环,但我完全迷失了。我是 Python 的新手,所以帮助会非常好!
def ReturnConsonants(string):
newStr=""
vowels="aeiouAEIOU"
for i in string:
if not(i in vowels):
newStr+=i
return(newStr)
string=input("enter word: ")
print(ReturnConsonants(string))
答案 0 :(得分:1)
例如:
j = 0
while j < len(string):
i = string[j]
if not (i in vowels):
newStr += i
j += 1
答案 1 :(得分:0)
这是您使用 while 循环的代码:
def ReturnConsonants(string):
newStr=""
vowels="aeiouAEIOU"
i=0
while i < len(string):
if not(string[i] in vowels):
newStr+=string[i]
i=i+1
return(newStr)
string=input("enter word: ")
print(ReturnConsonants(string))
您可以通过多种方式做到这一点。祝你好运。