我试图打印出bool结构范围内的素数。
def get_boundries_from_user():
upper_boundary = input("Enter upper boundary please:\n")
lower_boundary = input("Enter lower boundary please:\n")
return upper_boundry,lower_boundary
def print_prime_numbers():
upper_boundry,lower_boundry = get_boundries_from_user()
upper_boundry += 1
if (lower_boundary<0 or upper_boundary<0):
print "Negative values cannot be prime numbers"
return
for i in range(lower_boundary,upper_boundary):
prime = True
for k in range(2,i):
if (i%k==0):
prime = False
if prime:
print i
print_prime_numbers()
我错在哪里?你能解释一下你的答案的逻辑吗?
答案 0 :(得分:0)
def get_boundries_from_user():
upper_boundary = input("Enter upper boundary please:\n")
lower_boundary = input("Enter lower boundary please:\n")
return upper_boundary,lower_boundary
def print_prime_numbers():
upper_boundry,lower_boundry = get_boundries_from_user()
if (lower_boundry<0 or upper_boundry<0):
print "Negative values cannot be prime numbers"
return
print "\nPrimes"
for i in range(lower_boundry,upper_boundry+1):
prime = True
for k in range(2,i):
if (i%k==0):
prime = False
if prime:
print i
print_prime_numbers()
这是工作代码。请注意,您必须先输入上边界,然后输入下边界。因此,例如UB-20和LB-10将为您提供输出 素数 11 13 17 19