简体   繁体   English

请帮助理解 Python 中的代码

[英]Please Help Understand this Code in Python

I'm trying to polish my python skills and this made me confused.我正在努力提高我的 python 技能,这让我很困惑。 Here's the code:这是代码:

greeting = 'Hello!'
count = 0

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

print('done')

I don't understand what the count does.我不明白计数是做什么的。

this exact code doubles letters which position number is even.这个确切的代码将 position 编号为偶数的字母加倍。 count is for counting letter position. count 用于计数字母 position。 condition count % 2 == 0 to know which position is divided by 2 without leftovers (check if position is even number)条件计数 % 2 == 0 知道哪个 position 除以 2 没有剩余(检查 position 是否为偶数)

Let's comment up the code with explanations of what each line does and then examine the output.让我们注释代码并解释每行的作用,然后检查 output。

# assign the string 'Hello!' to the name greeting
greeting = 'Hello!'
# assign the number 0 to the name count
count = 0

# assign letter to each character in greeting one at a time.
# in the first run of the loop, letter would be 'H', then 'e',
# etc.
for letter in greeting:
    # add one to count. count starts at 0, so after this line
    # on the first run of the loop, it becomes 1, then 2, etc.
    count += 1
    # use the modulo operator (%) to get the remainder after
    # dividing count by 2, and compare the result to 0. this
    # is a standard way of determining if a number is even.
    # the condition will evaluate to True at 0, 2, 4, etc.
    if count % 2 == 0:
        # when count is even, print letter once followed by a
        # new line. note that to not print a new line, you can
        # write print(letter, end="")
        print(letter)
    # print letter once followed by a new line regardless of the
    # value of count
    print(letter)

# print 'done' followed by a new line
print('done')

So, based on all that, what should we expect the output to be?那么,基于所有这些,我们应该期望 output 是什么? Well, we know that every character of greeting will be printed in the last line of the loop.好吧,我们知道greeting的每个字符都会在循环的最后一行打印出来。 We also know that every even character of greeting will be printed twice, once inside the if block, and once at the end of the loop.我们还知道, greeting的每个偶数字符都会被打印两次,一次在if块内一次在循环结束时。 Finally, we know that "done" will be printed after the loop is complete.最后,我们知道循环完成后会打印“done”。 Therefore, we should expect the following output:因此,我们应该期待以下 output:

H
e
e
l
l
l
o
!
!
done

This this is very simple see:这很简单,请参见:

greeting = 'Hello!'

In this line you assigned the variable greeting a string value Hello!在这一行中,您为变量greeting分配了一个字符串值Hello! . .

count = 0

In this line you assigned the variable count a integer value 0 .在这一行中,您为变量count分配了 integer 值0 Now the next code is:现在下一个代码是:

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

As you may know strings are iterable.你可能知道字符串是可迭代的。 See this code before understanding the above:在了解上述内容之前,请查看此代码:

for letter in greeting:
    print(letter)

Forget the count.忘记计数。 At each iteration of the loop the loop is printing every character of the greeting.在循环的每次迭代中,循环都会打印问候语的每个字符。 so the output is:所以 output 是:

H
e
l
l
o
!

But why there a newline after each letter.但是为什么每个字母后面都有一个换行符。 The answer is that print has an optional argument end If the above code is like this:答案是 print 有一个可选参数end如果上面的代码是这样的:

    for letter in greeting:
       print(letter, end = '')

Then the output will be:那么 output 将是:

Hello!

Now there comes the count term.现在出现了计数项。 It is also simple.这也很简单。 when the count is even it prints the letter iterating at that time in loop twice due to twice print statement.当计数为偶数时,由于两次打印语句,它会在循环中打印两次迭代的字母。 And the count is even or not is checked by if count % 2 == 0 . if count % 2 == 0检查计数是否偶数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM