简体   繁体   English

从代码开始循环直到用户说停止在 Python

[英]Loop from the start of the code until the user says stop in Python

This is the question my teacher at school has given us:这是我在学校的老师给我们的问题:

Write a Python program to accept the value of an integer N and display all the factors of N. Repeat the above for multiple integers using a user-controlled loop.编写 Python 程序以接受 integer N 的值并显示 N 的所有因数。使用用户控制的循环对多个整数重复上述操作。

I'm not having any trouble with the main part of the code, however i don't understand how to loop it properly so that the code runs through the beginning again till the user says N at the end when prompted.我对代码的主要部分没有任何问题,但是我不明白如何正确循环它,以便代码再次从头开始运行,直到用户在提示时最后说 N。

This is my code:这是我的代码:

#this is the main part of the code
def print_factors(x):
    print("The factors of",x,"are: ")
    for i in range(1,x+1):
        if x%i==0:
            print(i)

#this is the error handling part of the code
while True:
    try:
        n=int(input("Please enter a number: "))
    except ValueError:
        print("Please enter a valid number.")
        continue
    else:
        break
    
print_factors(n)
#this is the looping part where i am having trouble
N = input("Do you want to continue to find factors Y/N: ").upper()
while True:
    while N not in 'YN':
            if N == 'Y':
                print_factors(int(input("Enter a number: ")))
            elif N == 'N':
                break
            else:
                print("Invalid input, please try again.")
                N = input("Do you want to continue to find factors Y/N: ").upper()
                print_factors(int(input("Enter a number: ")))

I want the code to go back to the start and ask for input again, and then ask if the user wants to continue and so on.我想把 go 的代码回到开始并再次要求输入,然后询问用户是否要继续等等。 But when I got to the end, the loop shows these results:但是当我走到最后时,循环显示了这些结果:

Do you want to continue to find factors Y/N: e
Invalid input, please try again.
Do you want to continue to find factors Y/N: y
Enter a number: 42
The factors of 42 are: 
1
2
3
6
7
14
21
42

If I type something other than y, then it also works, and ends after looping only once.如果我输入 y 以外的其他内容,那么它也可以工作,并且仅在循环一次后结束。 I want it to loop endlessly till the user gives the command to stop with a 'y' or 'Y' input and show an error message in all other cases.我希望它无限循环,直到用户给出命令以“y”或“Y”输入停止并在所有其他情况下显示错误消息。

I solve your problem by moving your inner while loop into the else case.我通过将您的内部while循环移动到else案例来解决您的问题。 Also, in the if statement N == 'Y' , I insert one more N = input(...) command:此外,在if语句N == 'Y'中,我再插入一个N = input(...)命令:

N = input("Do you want to continue to find factors Y/N: ").upper()
while True:
    if N == 'Y':
        print_factors(int(input("Enter a number: ")))
        N = input("Do you want to continue to find factors Y/N: ").upper()
        
    elif N == 'N':
        break
    else:
        while N not in 'YN':
            print("Invalid input, please try again.")
            N = input("Do you want to continue to find factors Y/N: ").upper()

Result:结果:

Please enter a number: 15
The factors of 15 are: 
1
3
5
15
Do you want to continue to find factors Y/N: y
Enter a number: 23
The factors of 23 are: 
1
23
Do you want to continue to find factors Y/N: e
Invalid input, please try again.
Do you want to continue to find factors Y/N: y
Enter a number: 32
The factors of 32 are: 
1
2
4
8
16
32
Do you want to continue to find factors Y/N: n
>>>
  • Side note : running on Python 3.9.1, Window 10.旁注:在 Python 3.9.1、Window 10 上运行。
  • Maybe you would want to include the same try-catch block into the user-controlled while loop.也许您希望将相同的try-catch块包含到用户控制的while循环中。

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

相关问题 Python 循环从 class 创建对象,直到用户说停止 - Python loop to create objects from class untill user says to stop 使用python从代码中的其他部分开始和停止while循环 - start and stop while loop from other part in the code using python Python:2输入变量循环会一直运行直到用户告诉它停止? - Python: 2-input-variable loop that operates until the user tells it to stop? Python tkinter启动和停止循环? - Python tkinter start and stop loop? Python while 循环提示直到输入停止 - Python while loop to prompt until stop is entered Python:是否可以从另一个线程启动和停止 while 循环? - Python: is it possible to start and stop a while loop from another thread? 如何停止循环,直到用户输入要停止的关键字? - How to stop loop until user inputs a keyword to stop? 停止 Python 程序,直到用户执行操作 - Stop a Python program until a user performs an action 如何循环列表,直到列表中的所有项目都与用户输入匹配,然后在 python 中使用宾果游戏消息停止循环 - how to loop a list until all items in a list are matched with user input then stop the loop with bingo message for bingo game in python 有人可以从“start + start % 2”的代码中解释一下这个片段吗? - Can Somebody explain me this snippet from code that says "start + start % 2"?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM