Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.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新手,这里是Python迭代_Python_String_Iteration - Fatal编程技术网

Python新手,这里是Python迭代

Python新手,这里是Python迭代,python,string,iteration,Python,String,Iteration,Python新手-你能解释一下吗 greeting = 'Hello!' count = 0 for letter in greeting: count += 1 if count % 2 == 0: print(letter) print(letter) print('done') 为了避免缩进错误,我将解释代码的作用 在您的程序中,您已经初始化了一个名为greeting的变量,该变量的值为“Hello!”,并且还初始化了一个值为0的

Python新手-你能解释一下吗

greeting = 'Hello!' 
count = 0  
for letter in greeting:
     count += 1   
  if count % 2 == 0: 
        print(letter) 
    print(letter) 
 print('done')

为了避免缩进错误,我将解释代码的作用

在您的程序中,您已经初始化了一个名为greeting的变量,该变量的值为“Hello!”,并且还初始化了一个值为0的计数

问候语=‘你好!’ 计数=0

此后,for-us使用了一个循环,该循环贯穿问候语,即直到每个单词Hello。但是,如果你想自己检查,你可以打印这封信

问候信: 印刷品(信件)

现在来看看您的问题,您还将count的值增加了1,其中每次循环执行时该值增加1

然后,您有一个条件来检查数字是否为偶数计数%2==0,然后是条件成功时执行的print语句。这意味着处于偶数位置的字母只能打印出来


这就是你的程序所做的。

greeting='Hello!'<代码>问候语=‘你好!’有相关的教程。该代码将不会运行,因为它充满了缩进错误。正确的缩进在Python中非常重要。请先尝试更正缩进。将程序保存到hello.py中,然后运行python hello.py检查结果。在这之后,您可以使用您的代码来理解它的行为。
greeting = 'Hello!' <-- set greeting variable.
count = 0  <-- set count variable.
for letter in greeting: <-- this loop will loop six times because greeting contains of six character.
     count += 1  <-- each times of loop will increase value of count by one.
  if count % 2 == 0: <-- this line will print a index of character that % 2 = 0 (eg. 2%2 = 0, 4%2 = 0, ...)
        print(letter) 
    print(letter) <-- this line will print any index of character of greeting. (ps. this line will error because indentation errors.)
 print('done') <-- print 'done'.
greeting = 'Hello!' <-- set greeting variable.
count = 0  <-- set count variable.
for letter in greeting: <-- this loop will loop six times because greeting contains of six character.
     count += 1  <-- each times of loop will increase value of count by one.
     if count % 2 == 0: <-- this line will print a index of character that % 2 = 0 (eg. 2%2 = 0, 4%2 = 0, ...)
        print(letter) 
    print(letter) <-- this line will print any index of character of greeting. (ps. this line will error because indentation errors.)
print('done') <-- print 'done'.