我正在创建一个程序,用户可以在其中输入 4 到 5000 之间的偶数,程序将输出一个素数对列表,这些素数对输入中提供的数字求和。
我的程序可以很好地将所有素数放在一个列表中,并检查输入是否为 4 到 5000 之间的偶数。对于小输入,它工作得很好,但对于较大的输入,它说“列表索引超出范围”和我不知道为什么。
这是我使用的代码:(添加注释以提供帮助)
global prime_numbers
prime_numbers=[]
user_number=0
def start():
for i in range(5000):
prime_test(i)
def prime_test(a):
global prime_numbers
x = True
for i in range(2, a):
while x:
if a%i == 0:
x = False
break
else:
x = True
break
if x==True and a!=0 and a!=1:
prime_numbers.append(a)
#print(prime_numbers[int(len(prime_numbers)-1)])#
#prints last digit/\ /\ /\ (for testing purposes)#
if a==4999:
user_input()
def user_input():
user_number=input('please enter a whole even number between 4 and 5000: ')
#checks if user input is valid#
try:
#checks for a number#
user_number=float(user_number)
#checks for an integar#
if user_number!=int(user_number):
print('that is not a whole number')
user_input()
#checks for an even number#
a=(user_number/2)
if a!=int(a):
print('that is not an even number')
user_input()
#checks it is inbetween 4 and 5000#
elif user_number<4:
print('that is not greater than or equal to 4')
user_input()
elif user_number>5000:
print('that is not less than or equal to 5000')
user_input()
#if it is a valid input it procedes to the next section#
else:
q=int(user_number)
goldbergs_conjecture(q)
#the bit that checks to see if it is a number#
except ValueError:
print('that is not a number')
user_input()
def goldbergs_conjecture(q):
global prime_numbers
for i in range(q):
#serches for the first prime number in the list then the second ect...#
x=prime_numbers[i]
#y= user input minus a prime number form the list#
y=q-x
#if y is also in the list then it must be prime also so these are printed#
if y in prime_numbers and x<(q/2):
print(str(x) + ' + ' + str(y))
#had to add this seperatly because if they were the same then this /\/\ didnt catch them#
if y==x:
print(str(x) + ' + ' + str(y))
#gives the user something to look at whilst the list is generating#
print('loading...')
start()
对于少数人来说,这非常有效,例如
>>>
RESTART: C:\!A-level cs preparation\9th problem, goldbach's conjecture\main code first attempt.py
loading...
please enter a whole even number between 4 and 5000: 568
5 + 563
11 + 557
47 + 521
59 + 509
89 + 479
101 + 467
107 + 461
137 + 431
149 + 419
167 + 401
179 + 389
251 + 317
257 + 311
>>>
但是对于更大的数字,例如 800,(实际上它是任何高于 668 的数字,如果有帮助的话)然后它会显示“IndexError:列表索引超出范围”
>>>
RESTART: C:\!A-level cs preparation\9th problem, goldbach's conjecture\main code first attempt.py
loading...
please enter a whole even number between 4 and 5000: 800
3 + 797
13 + 787
31 + 769
43 + 757
61 + 739
67 + 733
73 + 727
109 + 691
127 + 673
139 + 661
157 + 643
181 + 619
193 + 607
199 + 601
223 + 577
229 + 571
277 + 523
313 + 487
337 + 463
367 + 433
379 + 421
Traceback (most recent call last):
File "C:\!A-level cs preparation\9th problem, goldbach's conjecture\main code first attempt.py", line 72, in <module>
start()
File "C:\!A-level cs preparation\9th problem, goldbach's conjecture\main code first attempt.py", line 6, in start
prime_test(i)
File "C:\!A-level cs preparation\9th problem, goldbach's conjecture\main code first attempt.py", line 26, in prime_test
user_input()
File "C:\!A-level cs preparation\9th problem, goldbach's conjecture\main code first attempt.py", line 52, in user_input
goldbergs_conjecture(q)
File "C:\!A-level cs preparation\9th problem, goldbach's conjecture\main code first attempt.py", line 61, in goldbergs_conjecture
x=prime_numbers[i]
IndexError: list index out of range
>>>
我不知道为什么这在 668 之后不起作用。