Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.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 loop 我如何重写a';对于循环';作为';while循环';用python?_For Loop_While Loop - Fatal编程技术网

For loop 我如何重写a';对于循环';作为';while循环';用python?

For loop 我如何重写a';对于循环';作为';while循环';用python?,for-loop,while-loop,For Loop,While Loop,这是一个循环: for c in "Hi there": print(c.upper()) 这是我作为while循环重写的尝试: phrase = "Hi there" while len.phrase > 0 print (c.upper()) 使用迭代器 phrase = "Hi there" limit = len(phrase); itery =0; while itery<limit: print (phrase[itery].upper())

这是一个循环:

for c in "Hi there":
     print(c.upper())
这是我作为while循环重写的尝试:

phrase = "Hi there"
while len.phrase > 0
    print (c.upper())

使用迭代器

phrase = "Hi there"
limit = len(phrase);
itery =0;
while itery<limit:
    print (phrase[itery].upper());
    itery+=1;
短语=“你好”
限制=长度(短语);
itery=0;

虽然itery这里有几件事我看错了

首先,您忘了在while语句末尾使用冒号

while len.phrase > 0:
其次,执行此任务的更有效方法如下:

while 1:
因为1永远是真的(在Python的情况下),它将执行一个无限循环,就像您之前的尝试似乎打算做的那样