5 Replies - 659 Views - Last Post: 12 December 2012 - 02:26 AM Rate Topic: -----

#1 Stephenfrazier10  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • 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  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#3 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2102
  • View blog
  • Posts: 3,207
  • 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.
Was This Post Helpful? 1
  • +
  • -

#4 Stephenfrazier10  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#5 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2102
  • View blog
  • Posts: 3,207
  • 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.
Was This Post Helpful? 1
  • +
  • -

#6 Stephenfrazier10  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1