# List Index out of range Error

Page 1 of 1

## 5 Replies - 1138 Views - Last Post: 12 December 2012 - 02:26 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=303694&amp;s=b4c94b9a27a3643c6d44a992b4373755&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Stephenfrazier10

Reputation: 2
• Posts: 109
• Joined: 12-September 12

# 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.

```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_number-1]
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(computersGuess-1, 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.")

```

Is This A Good Question/Topic? 0

## Replies To: List Index out of range Error

### #2 Stephenfrazier10

Reputation: 2
• Posts: 109
• Joined: 12-September 12

## 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

### #3 sepp2k

• D.I.C Lover

Reputation: 2723
• Posts: 4,349
• Joined: 21-June 11

## 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 Stephenfrazier10

Reputation: 2
• Posts: 109
• Joined: 12-September 12

## 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

### #5 sepp2k

• D.I.C Lover

Reputation: 2723
• Posts: 4,349
• Joined: 21-June 11

## 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 Stephenfrazier10

Reputation: 2
• Posts: 109
• Joined: 12-September 12

## 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.