Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.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
Python 将for循环转换为while循环_Python - Fatal编程技术网

Python 将for循环转换为while循环

Python 将for循环转换为while循环,python,Python,如何将这些for循环转换为while循环?您缺少for和while循环的要点 基本上,for循环在列表中迭代,您可以在执行操作时对项目进行操作 相反,while循环用于运行直到满足条件,例如触发标志 for循环: 返回: mylist = ["this", "is", "a", "for", "loop"] for element in mylist: print(element) count = 0 while count != 10: print(count) co

如何将这些for循环转换为while循环?

您缺少for和while循环的要点

基本上,for循环在列表中迭代,您可以在执行操作时对项目进行操作

相反,while循环用于运行直到满足条件,例如触发标志

for循环:

返回:

mylist = ["this", "is", "a", "for", "loop"]
for element in mylist:
    print(element)
count = 0
while count != 10:
    print(count)
    count += 1
print("count has reached 10")
其中,作为while循环:

this
is
a
while
loop
返回:

mylist = ["this", "is", "a", "for", "loop"]
for element in mylist:
    print(element)
count = 0
while count != 10:
    print(count)
    count += 1
print("count has reached 10")

总之,for循环用于迭代数组或生成器对象,其中as-while循环用于运行,直到满足条件。

为什么要将其转换为while循环?你在使用for循环时遇到了什么问题?while循环与for循环的比较是什么?我只是好奇你会怎么做。我做过其他的转换,但这一次让我很难堪。可能是重复的