Python Forum
Hello Everyone! Possible bug?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Hello Everyone! Possible bug?
#1
My name is Nico,
I'm programmer i search possible answer for this code!
numeri = [i for i in range(1,91)]
giusti = []
utente = input("Inserisci i numeri separati da vergola")
for i in utente.split(","):
	if(i.isdecimal()):
		if(int(i)<=90 and int(i)>=1):
			print("Debug il numero è giusto")
			giusti.append(i)
			numeri.pop(int(i))
		else:
			print("il numero è sbagliato")
print("I numeri sono esatti")
Enter the sequence at the code launch 1,24,90,99,8

i recived error!
this is output:

Inserisci i numeri separati da vergola 1,24,90,99,8
Debug il numero è giusto
Debug il numero è giusto
Debug il numero è giusto
Traceback (most recent call last):
  File "programma.py", line 9, in <module>
    numeri.pop(int(i))
IndexError: pop index out of range
if you follow the program, you will understand that it runs .pop for number 99, this is strange.. with other sequence numbers does not happen

tried it python 3.6.2 and 3.5.3 Linux

P.S. don't use IDE for python simple texteditor
Reply
#2
The first time you pop an item off of the list: 'numeri'
, (which is item 1) you change the indexing order.
Therefore on the second iteration when i = 24, you are not removing the number 24 from the list, but
are removing index number 24 which now contains the number 25 and so forth until finally you request
to pop an index that not longer exists, and thus the error.

Therefore not a bug at all!
Reply
#3
line 6,
if(int(i)<=90 and int(i)>=1):
the question is how does it generate true with 99?
99 <= 90? yes
and
99 >= 1? yes...

should not you escape into the else block?

sorry!
99<= 90 No
99>= 1 yes
:)

Sorryyyy!!!

.pop(index) is not remove(value)
ahaah
i was confused .pop with .remove()
ahahahahah



I think I'm crazy with work

sorry for the disorder
thank you very much for the answer
Reply
#4
What you are comparing is i. The index is not the problem.
The problem is that the size of the list is shrinking.
I still remains 1 through 90
The program crashes on the third iteration, when i = 90
If you have a debugger, you can see this, if not, you can see it with
your code, and slight modification as below.
execution will stop on each iteration after it prints out the value of i, and the length of numeri:
numeri = [i for i in range(1,91)]
giusti =
utente = input("Inserisci i numeri separati da vergola: ")
for i in utente.split(","):
    if(i.isdecimal()):
        if(int(i)<=90 and int(i)>=1):
            print("Debug il numero è giusto")
            giusti.append(i)
            print('i = {}, and length of numeri = {}'.format(i, len(numeri)))
            input('Press enter to pop this item and start next iteration')
            numeri.pop(int(i))
        else:
            print("il numero è sbagliato")
print("I numeri sono esatti")
Reply
#5
yes!, i have understand the problem, i was confused .pop() with .remove () a distraction, fatigue and confusion make me a joke. Dodgy Dodgy Dodgy
Reply
#6
No, I've done it as well.
It's like == in C and C++,
though I used 'C' for thirty years I still sometimes got caught by that one.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020