selfteaching / selfteaching/selfteaching-python-camp

Day04 资料2中的for in 嵌套循环看不懂了,不知道解决思路或是卡在哪里了

Open
#4,088 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
151
Forks
875
PR merge metrics
No merged PRs in 30d

Description

for n in range(2, 10):
     for x in range(2, n):
         if n % x == 0:
             print(n, 'equals', x, '*', n//x)
             break
     else:
         # loop fell through without finding a factor
         print(n, 'is a prime number')
D3FB3DDA-6ABA-459e-AF63-F2C2EFB1F270

看不懂这个嵌套循环了,程序第一次扫描的时候,n%x==0,n等于2,x等于2。2%2的余数等于0。那应该显示print(n, 'is a prime number')这段,

下次循环,怎么会跳出来,到 else:这段呢?n=3 x=2 余数不等于0,显示print(n, 'equals', x, '*', n//x)。n=3 x=3 余数等于0,显示print(n, 'is a prime number')这段

下次循环n=4 x=2 余数不等于0,显示print(n, 'equals', x, '*', n//x)。n=4 x=3 余数不等于0,显示print(n, 'is a prime number')这段 n=4 x=4 余数等于0,显示print(n, 'is a prime number')这段
晕了

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the Day04 资料2 material and reviewing the embedded Python example. Clarify the nested loop and its loop-else behavior in the material, with a step-by-step explanation that resolves the reported confusion and makes the expected output understandable.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.