from random import * def ComputerGuesser(): numList = [] for i in range(1,101): numList.append(int(i)) the_number = int(input("Pick a number between 1 and 100: ")) print("\n") the_number = numList[the_number1] computersGuess = randint(1,100) tries = 0 while computersGuess != the_number: if computersGuess > the_number: print("Computers guess: " + str(computersGuess)) print("It is greater than your guess. Go lower computer.") for i in range(computersGuess1, len(numList)1): numList.remove(numList[i]) computersGuess = randint(computersGuess[0],len(numList)) tries += 1 else: print("At last, the computer has guessed your number!") print("It was " + str(the_number)) print("And it did this in " + str(tries) + " tries.")
List Index out of range Error
Page 1 of 15 Replies  710 Views  Last Post: 12 December 2012  02:26 AM
#1
List Index out of range Error
Posted 12 December 2012  01:49 AM
Im trying to create a program where the user picks a number between 1 and 100 and the computer tries to guess it. Every time the computer makes a guess, it goes into a if statements and sees if the guess is greater than the number or not. I am working on the greater than portion right now, and I want it to see if its guess is larger than the users entered number, and if it is remove all of the numbers from the list that are greater than it. I thought I figured it out but I keep getting a list out of range error.
Replies To: List Index out of range Error
#2
Re: List Index out of range Error
Posted 12 December 2012  01:58 AM
Okay So I took the numList[i] out and made it just numList.remove(i)
Now its giving me a new error:
computersGuess = randint(computersGuess[0],len(numList))
TypeError: 'int' object is not subscriptable
Now its giving me a new error:
computersGuess = randint(computersGuess[0],len(numList))
TypeError: 'int' object is not subscriptable
#3
Re: List Index out of range Error
Posted 12 December 2012  02:03 AM
Yes, that's because computersGuess is an int, not a list (or anything else that is subscriptable), so writing computersGuess[0] doesn't work.
#4
Re: List Index out of range Error
Posted 12 December 2012  02:09 AM
Wow, I feel like an idiot. I have no idea how I didnt catch that...
...Now I have another problem it the computers guess stays the same...I dont get it, since the computer creates a new guess each time the loop goes through
...Now I have another problem it the computers guess stays the same...I dont get it, since the computer creates a new guess each time the loop goes through
#5
Re: List Index out of range Error
Posted 12 December 2012  02:24 AM
Okay, so let's play this through. The computer picks his first number. Then that number and all numbers greater than it except 100 are removed from that list. len(numList) will now be equal to computerGuess because numList contains all the numbers below computerGuess as well as 100, making a total of computerGuess numbers. Then you tell it to pick a new random number between computerGuess and len(numList). Since those numbers are now equal, the only number it can pick is computerGuess.
#6
Re: List Index out of range Error
Posted 12 December 2012  02:26 AM
I switched it to
computersGuess = randint(numList[0],len(numList))
I also added a new post, since the name of this one really does not make sense anymore.
computersGuess = randint(numList[0],len(numList))
I also added a new post, since the name of this one really does not make sense anymore.
Page 1 of 1
