Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/svn/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将这个for循环转换为while循环(Python)?_Python - Fatal编程技术网

如何将这个for循环转换为while循环(Python)?

如何将这个for循环转换为while循环(Python)?,python,Python,我目前正试图找到一种方法,将这个for循环改为while循环,但我完全迷路了。我对python这整件事还不熟悉,所以帮助我真的很好 def ReturnConsonants(string): newStr="" vowels="aeiouAEIOU" for i in string: if not(i in vowels): newStr+=i return(newStr) strin

我目前正试图找到一种方法,将这个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))
例如:

j = 0
while j < len(string):
    i = string[j]
    if not (i in vowels):
        newStr += i
    j += 1
j=0
而j
以下是使用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))
def返回辅音(字符串):
newStr=“”
元音=“aeiouAEIOU”
i=0
当我

你可以用很多方法来做。祝你好运