哥德巴赫猜想算法显示 "list index out of range" 以上特定数字
goldbach's conjecture algorithm shows "list index out of range" above a specific number
我正在创建一个程序,用户可以在其中输入一个 4 到 5000 之间的偶数,该程序将输出一个素数对列表,这些素数对输入中提供的数字求和。
我的程序工作正常,可以将所有质数放入列表中,并检查输入是否为 4 到 5000 之间的偶数。对于小输入,它工作得很好,但对于更大的输入,它说 "list index is out of range" 我不知道为什么。
这是我使用的代码:(已添加注释以提供帮助)
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 preparationth 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: list index out of range"
>>>
RESTART: C:\!A-level cs preparationth 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 preparationth problem, goldbach's conjecture\main code first attempt.py", line 72, in <module>
start()
File "C:\!A-level cs preparationth problem, goldbach's conjecture\main code first attempt.py", line 6, in start
prime_test(i)
File "C:\!A-level cs preparationth problem, goldbach's conjecture\main code first attempt.py", line 26, in prime_test
user_input()
File "C:\!A-level cs preparationth problem, goldbach's conjecture\main code first attempt.py", line 52, in user_input
goldbergs_conjecture(q)
File "C:\!A-level cs preparationth 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 之后不起作用。
因为0到5000之间的质数个数不到5000个就坏了(如果你没有说错条件的话应该是不到668个)。我建议在第 59 行使用 range(len(prime_numbers)) 而不是 range(q) (您也可以使用 enumerate
函数)
您在 range(q)
上循环(在 goldbergs_conjecture()
中),可以是 4..5000.
之间的任意值
但您正在寻找此值:x=prime_numbers[i]
,它是 prime_numbers
列表的索引。
不保证 prime_numbers
列表与 range(q)
一样长,因为(根据定义)素数比整数少 一个给定的范围。
尝试在请求输入之前打印出 prime_numbers
列表的长度。我敢打赌它接近 668。
相反,您可以尝试类似的方法:
for prime in prime_numbers:
if prime >= q:
break
... do something with prime
遍历所有小于q
的素数。
所以,我最重要的建议是:除非你真的需要,否则不要使用 try。它将隐藏代码错误。除非你真的需要,否则避免使用全局变量。一会就头晕脑胀
在你的代码中,第二个错误是问题的根源。您正在 运行 范围 (q) 中指向 prime_numbers[i] 的 for 循环。绑定这些索引意味着两个列表需要具有相同的大小,或者素数应该小于 q。在 for i in range(q)
循环的开头写入 print(i)
和 print(len(prime_numbers))。你会看到脚本在 i > len(prime_numbers)
时中断
为避免这种尝试,请为您的输入编写正则表达式。
为避免全局,将列表作为函数参数传递,或 运行 prime_number 列表上的循环。
希望对您有所帮助。
我正在创建一个程序,用户可以在其中输入一个 4 到 5000 之间的偶数,该程序将输出一个素数对列表,这些素数对输入中提供的数字求和。
我的程序工作正常,可以将所有质数放入列表中,并检查输入是否为 4 到 5000 之间的偶数。对于小输入,它工作得很好,但对于更大的输入,它说 "list index is out of range" 我不知道为什么。
这是我使用的代码:(已添加注释以提供帮助)
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 preparationth 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: list index out of range"
>>>
RESTART: C:\!A-level cs preparationth 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 preparationth problem, goldbach's conjecture\main code first attempt.py", line 72, in <module>
start()
File "C:\!A-level cs preparationth problem, goldbach's conjecture\main code first attempt.py", line 6, in start
prime_test(i)
File "C:\!A-level cs preparationth problem, goldbach's conjecture\main code first attempt.py", line 26, in prime_test
user_input()
File "C:\!A-level cs preparationth problem, goldbach's conjecture\main code first attempt.py", line 52, in user_input
goldbergs_conjecture(q)
File "C:\!A-level cs preparationth 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 之后不起作用。
因为0到5000之间的质数个数不到5000个就坏了(如果你没有说错条件的话应该是不到668个)。我建议在第 59 行使用 range(len(prime_numbers)) 而不是 range(q) (您也可以使用 enumerate
函数)
您在 range(q)
上循环(在 goldbergs_conjecture()
中),可以是 4..5000.
但您正在寻找此值:x=prime_numbers[i]
,它是 prime_numbers
列表的索引。
不保证 prime_numbers
列表与 range(q)
一样长,因为(根据定义)素数比整数少 一个给定的范围。
尝试在请求输入之前打印出 prime_numbers
列表的长度。我敢打赌它接近 668。
相反,您可以尝试类似的方法:
for prime in prime_numbers:
if prime >= q:
break
... do something with prime
遍历所有小于q
的素数。
所以,我最重要的建议是:除非你真的需要,否则不要使用 try。它将隐藏代码错误。除非你真的需要,否则避免使用全局变量。一会就头晕脑胀
在你的代码中,第二个错误是问题的根源。您正在 运行 范围 (q) 中指向 prime_numbers[i] 的 for 循环。绑定这些索引意味着两个列表需要具有相同的大小,或者素数应该小于 q。在 for i in range(q)
循环的开头写入 print(i)
和 print(len(prime_numbers))。你会看到脚本在 i > len(prime_numbers)
为避免这种尝试,请为您的输入编写正则表达式。 为避免全局,将列表作为函数参数传递,或 运行 prime_number 列表上的循环。
希望对您有所帮助。