本文介绍了关于Python循环的一个简单问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了两个循环样本,但是我知道为什么当loop1从3开始,loop2从2开始? ı应该两者都应该是一样的。





这是输出:





3 prime

4 = 2 * 2.0

5 prime

2 prime

3 prime

4 = 2 * 2.0

5 prime



我尝试了什么:



< pre lang =   Python> 



 def loop1():
表示范围内的n(2,6):
表示范围内的x(2,n):
如果n%x == 0:
print(n,=,x ,*,n / x)
else:
print(n,prime)

break
loop1()

def loop2():
表示范围(2,6)中的c:
表示范围内的k(2,c):
如果c%k == 0:
print(c,=,k,*,c / k)
break
else:
print(c,prime)
loop2()

解决方案


I've tried two loops samples but ı didint understand why when"loop1" starts from "3", "loop2" starts from "2"? ı supposed that both should have been the same.


Here is the output:


3 prime
4 = 2 * 2.0
5 prime
2 prime
3 prime
4 = 2 * 2.0
5 prime

What I have tried:

<pre lang="Python">


def loop1():
    for n in range (2,6):
        for x in range(2,n):
            if n % x==0:
                print(n,"=",x,"*",n/x)
            else:
                print(n,"prime")

            break
loop1()

def loop2():
    for c in range (2,6):
        for k in range(2,c):
            if c % k==0:
                print(c,"=",k,"*",c/k)
                break
        else:
                print(c,"prime")
loop2()
解决方案


这篇关于关于Python循环的一个简单问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 09:44