如何在for循环中再次读取输入?

时间:2019-03-26 10:17:02

标签: python

我是Python的新生,现在遇到一个我无法理解的问题。如何在for循环之外调用输入?

请一些高手使用以下示例给我答案。谢谢!


n=int(input('Please input a number: ')

for i in range(2,n):

    if n%i==0:
        print('It is not a prime number!',  end=' ')
        break
    else:
        print('%d is a prime number!' % n)

2 个答案:

答案 0 :(得分:0)

只需在您的代码周围添加另一个循环即可。例如

while True:
    n=int(input('Please input a number: '))

    for i in range(2,n):
        if n%i==0:
            print('It is not a prime number!',  end=' ')
        else:
            print('%d is a prime number!' % n)
            break

答案 1 :(得分:0)

这是您要寻找的吗?

while True:
    n=int(input('Please input a number: ')
    flag = True
    for i in range(2,n):
        if n%i==0:
            flag = False
            break

    if flag == True:
        print('%d is a prime number!' % n)
        break
    else:
        print('It is not a prime number!',  end=' ')