[英]Python- checking a number to see if it's a prime number by using divisors
I have to write a program that checks if a number is a prime number by going through divisors in the range(1,num). 我必须编写一个程序,通过检查range(1,num)中的除数来检查数字是否为质数。 (For ex. if the input is 5, the program will check if 2 is a divisor, then 3, then 4).
(例如,如果输入为5,则程序将检查2是除数,然后是3,然后是4)。 Can someone explain how I can put the last expression in my program (the "this is a prime number" print function), so that it appears only after the "else" function in the "for" loop and not the "if div == 0:" statements?
有人可以解释一下如何将最后一个表达式放在程序中(“这是质数”打印函数),以便它仅出现在“ for”循环中的“ else”函数之后,而不出现在“ if div = = 0:“语句? Thank you!
谢谢!
while True:
num = int(input("Enter a positive number to see if it's a prime number: "))
if num > 1:
break
elif num == 1:
print("1 is technically not a prime number.")
else:
print("Number cannot be negative- try again.")
for num1 in range(2,num):
div = num%num1
if div == 0:
print(num1,"is a divisor of",num,"... stopping.")
print(num,"is not a prime number.")
break
else:
print(num1,"is NOT a divisor of",num,"... continuing")
print(num,"is a prime number!")
There are many ways to do this: 有很多方法可以做到这一点:
is_prime = True # a boolean flag
for num1 in range(2,num):
div = num%num1
if div == 0:
print(num1,"is a divisor of",num,"... stopping.")
print(num,"is not a prime number.")
is_prime = False
break
else:
print(num1,"is NOT a divisor of",num,"... continuing")
if is_prime:
print(num,"is a prime number!")
for-else
! for-else
使用! for num1 in range(2,num):
div = num%num1
if div == 0:
print(num1,"is a divisor of",num,"... stopping.")
print(num,"is not a prime number.")
is_prime = False
break
else:
print(num1,"is NOT a divisor of",num,"... continuing")
else:
print(num,"is a prime number!") # will execute only when for-loop ends
Python's for
expression has an optional else
clause : Python的
for
表达式具有可选的else
子句 :
Loop statements may have an else clause;
循环语句可以包含else子句; it is executed when the loop terminates through exhaustion of the list (with for) or when the condition becomes false (with while), but not when the loop is terminated by a break statement.
当循环通过用尽列表而终止时(带有for)或条件变为假(具有while时),则执行此命令,但当循环由break语句终止时,则不执行该命令。
So your code should look like this: 因此,您的代码应如下所示:
while True:
num = int(input("Enter a positive number to see if it's a prime number: "))
if num > 1:
break
elif num == 1:
print("1 is technically not a prime number.")
else:
print("Number cannot be negative- try again.")
for num1 in range(2,num):
div = num%num1
if div == 0:
print(num1,"is a divisor of",num,"... stopping.")
print(num,"is not a prime number.")
break
else:
print(num1,"is NOT a divisor of",num,"... continuing")
else:
print(num,"is a prime number!")
Given that you have already covered a division by 1, all you have to do is count if there is only one more divisor (the number itself). 假设您已将除数除以1,那么您所要做的就是计算除数以外的除数(数字本身)。 So to keep your code structure I added a counter.
因此,为了保持您的代码结构,我添加了一个计数器。
while True:
num = int(input("Enter a positive number to see if it's a prime number: "))
if num > 1:
break
elif num == 1:
print("1 is technically not a prime number.")
else:
print("Number cannot be negative- try again.")
for num1 in range(2,num):
div = num%num1
count = 0
if div == 0:
print(num1,"is a divisor of",num,"... stopping.")
print(num,"is not a prime number.")
break
else:
count += 1
print(num1,"is NOT a divisor of",num,"... continuing")
if count == 1:
print(num,"is a prime number!")
I added one line sys.exit() 我添加了一行sys.exit()
while True:
num = int(input("Enter a positive number to see if it's a prime number: "))
if num > 1:
break
elif num == 1:
print("1 is technically not a prime number.")
else:
print("Number cannot be negative- try again.")
for num1 in range(2,num):
div = num%num1
if div == 0:
print(num1,"is a divisor of",num,"... stopping.")
print(num,"is not a prime number.")
sys.exit()
else:
print(num1,"is NOT a divisor of",num,"... continuing")
print(num,"is a prime number!")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.