简体   繁体   English

如何循环回到Python中的开头

[英]How to loop back to the beginning in Python

I need a bit of help in finishing this code.我需要一些帮助来完成这段代码。 I am stuck trying to figure out what's wrong with my code.我一直在试图找出我的代码有什么问题。 I don't know how to loop back from the top using while loop.我不知道如何使用 while 循环从顶部循环回来。 It also wont print the last bit.它也不会打印最后一位。 I got the prime numbers settled but I don't know how the looping back works.我得到了质数,但我不知道循环是如何工作的。 May you help me?你能帮我吗?

while True:
    num=int(input("Enter a number:"))
    no="n"
    yes="y"
    
    if num >=1:
        for i in range(2, num):
            if (num % i) == 0:
                print(num,"is not a prime number")
                break
        else:
            print(num,"is a prime number")  
    x=str(input("If you want to continue press y, n if not:"))

if x==yes:
    print()
    #this part I don't know how to loop back to the start

elif x==no:        
        print("Okay")
    #and also the loop won't stop and print the text up there^^^ when I choose n

  
            

put the if in the while loop only, and break out of the while loop if no只将 if 放在 while 循环中,如果没有则跳出 while 循环

while True:
    ...
    x=str(input("If you want to continue press y, n if not:"))

    if x=="no":        
        print("Okay")
        break

If you want this to repeat infinitely, your code structure might look like this:如果您希望它无限重复,您的代码结构可能如下所示:

while True:  # The outer while loop, only ends when the player says "n"
    while True:
        # This is where you get the prime nums, paste the code for that here
    if x == "n":
        break  # This method will only stop the loop if the input is "no", else it keeps looping forever

Or, more simply, you can do this in just one loop.或者,更简单地说,您可以在一个循环中执行此操作。 The structure would be like this:结构将是这样的:

while True:
    # Get if the num is prime, paste the code for that

    if x == "n":
        break

Possible solution if the following:如果出现以下情况,可能的解决方案:

def is_prime(num):
    if num <= 1:
        return False
    if num % 2 == 0:
        return num == 2
    d = 3
    while d * d <= num and num % d != 0:
        d += 2
    return d * d > num
        

while True:
    num=int(input("Enter a number: "))
    
    if is_prime(num):
        print(f'{num} is a prime number')
    else:
        print(f'{num} is not a prime number')
        
    x = str(input("If you want to continue enter Y, or N if not: ")).lower()

    if x == 'y':
        print("let's start over...\n")

    elif x == 'n':      
        print("Okay, see you")
        break

Returns:退货:

![在此处输入图片描述

You can use code like this.您可以使用这样的代码。 In my opinion function more usefull for check prime numbers.在我看来 function 对于检查质数更有用。

def is_prime(x):
    for d in range(2, int(x**0.5)+1):
        if x % d == 0:
            return False
    return x > 1

no, yes = "n", "y"
x = yes
while x == yes:
    num=int(input("Enter a number:"))
    
    if is_prime(num):
        print(num,"is a prime number")
    else:
        print(num,"is not a prime number")

    x = input("If you want to continue press y, n if not:")

print("Okay")

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

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