10 Replies - 1999 Views - Last Post: 10 January 2014 - 07:49 PM Rate Topic: -----

#1 kjarva   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 14
  • Joined: 06-October 13

Guess the number game stuck in a while loop...

Posted 10 January 2014 - 05:17 PM

Hey all,

I'm new to python and have been working my way through python programming for the absolute beginner, 3rd Ed. I'm trying to code a simple guess the number game, where I picked a number and the game tries to guess it. But I seem to be stuck in the "my first guess is loop". So far I've only learned about while loops and if statements, so these are what I'm using for the game. Can some kind should take a look at my code and tell me what stupidly obvious thing I've done wrong?

import random

highnumber = 101
lownumber = 1
firstguess = random.randrange(1,101)
otherguess = random.randrange(lownumber,highnumber)
guess = False
userinput = ""

print("welcome to the guess a number game! Pick a number between 1 & 100 \n")

while guess != True:
    print("my first guess is: ",firstguess)
    print("Am I HIGHER, LOWER, or GOT_IT?\n")
    input(userinput)
    if userinput == "higher":
        lownumber = firstguess
        print("my next guess is: ",otherguess)
    elif userinput == "lower":
        highnumber = firstguess
        print("my next guess is: ",otherguess)
    elif userinput == "got it":
        print("I guessed right! Your number was",otherguess)
        guess = True 


Is This A Good Question/Topic? 0
  • +

Replies To: Guess the number game stuck in a while loop...

#2 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11041
  • View blog
  • Posts: 18,857
  • Joined: 19-March 11

Re: Guess the number game stuck in a while loop...

Posted 10 January 2014 - 05:33 PM

The problem here is that you're not storing the value that the user enters.

When you call input(foo), foo is the variable that's used as a prompt:

>>> foo = "Say hello: "
>>> input (foo)
Say hello: Hello!
'Hello!'
>>> 


If you want to use the value you have to store it to a variable:

>>> howdy = input (foo)
Say hello: Hello!
>>> howdy
'Hello!'



Hope that helps.
Was This Post Helpful? 1
  • +
  • -

#3 kjarva   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 14
  • Joined: 06-October 13

Re: Guess the number game stuck in a while loop...

Posted 10 January 2014 - 05:36 PM

:death: I can't believe I missed something so stupid! Thanks Jon!
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11041
  • View blog
  • Posts: 18,857
  • Joined: 19-March 11

Re: Guess the number game stuck in a while loop...

Posted 10 January 2014 - 05:37 PM

Glad I could help!
Was This Post Helpful? 0
  • +
  • -

#5 kjarva   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 14
  • Joined: 06-October 13

Re: Guess the number game stuck in a while loop...

Posted 10 January 2014 - 06:32 PM

Lol - back again! Ok my loop is now working as it should and it "appears" that the loop is working as it should but I'm hitting one issue. My random number isn't changing as it moves through the loop…

import random

highnumber = 101
lownumber = 1
guess = random.randrange(lownumber,highnumber)
won = False
userinput = ""

print("welcome to the guess a number game! Pick a number between 1 & 100 \n")
print("my first guess is: ",guess)
while won != True:
    userinput = input("Higher, Lower or Got It? \n")
    if userinput == "higher":
        lownumber = guess
        print("my next guess is: ",guess)
    elif userinput == "lower":
        highnumber = guess
        print("my next guess is: ",guess)
    elif userinput == "got it":
        print("I guessed right! Your number was",guess)
        won = True 


It spits out an initial random number, then asks about higher, lower or got it. No matter what I type at higher or lower, for some reason the random number isn't changing, even though I've set the randrange to change with each guess. Any pointers on what other stupid nebbish mistake I've made?
Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11041
  • View blog
  • Posts: 18,857
  • Joined: 19-March 11

Re: Guess the number game stuck in a while loop...

Posted 10 January 2014 - 06:37 PM

Is there any place within the while loop where you update the number?
Was This Post Helpful? 0
  • +
  • -

#7 kjarva   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 14
  • Joined: 06-October 13

Re: Guess the number game stuck in a while loop...

Posted 10 January 2014 - 06:41 PM

I thought that was what I was doing here:
if userinput == "higher":
        lownumber = guess


because I was changing the range the number should be picked from? Incorrect? since I have
guess = random.randrange(lownumber,high number)

Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11041
  • View blog
  • Posts: 18,857
  • Joined: 19-March 11

Re: Guess the number game stuck in a while loop...

Posted 10 January 2014 - 06:48 PM

This line
guess = random.randrange(lownumber,high number)



sets guess to some particular number, which is chosen at random and then doesn't change. In general, a variable's value is only going to change if it appears on the left hand side of an = sign.
Was This Post Helpful? 0
  • +
  • -

#9 kjarva   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 14
  • Joined: 06-October 13

Re: Guess the number game stuck in a while loop...

Posted 10 January 2014 - 06:56 PM

ah OK - I'll go read some more and see if I can figure this out then. Thanks again Jon :)
Was This Post Helpful? 0
  • +
  • -

#10 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11041
  • View blog
  • Posts: 18,857
  • Joined: 19-March 11

Re: Guess the number game stuck in a while loop...

Posted 10 January 2014 - 07:03 PM

Here's a tip: when you don't know what to do next, make a list of instructions. Suppose you were trying to teach an obedient but unimaginative and somewhat stupid person how solve this problem of picking the number that some volunteer has chosen. They're going to have a list of instructions, and they're going to follow them exactly. What do you tell them? Remember, this person has no initiative of their own, so if it's not in your instructions, they're not going to do it.
Was This Post Helpful? 1
  • +
  • -

#11 kjarva   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 14
  • Joined: 06-October 13

Re: Guess the number game stuck in a while loop...

Posted 10 January 2014 - 07:49 PM

Jon I got it!!

I just had to declare in the if statement that guess was a random number again :D
import random

highnumber = 101
lownumber = 1
guess = random.randrange(lownumber,highnumber)
won = False


print("welcome to the guess a number game! Pick a number between 1 & 100 \n")
print("my first guess is: ",guess)
while won != True:
    userinput = input("Higher, Lower or Got It? \n")
    if userinput == "higher":
        lownumber = guess
        guess = random.randrange(lownumber, highnumber)
        print("my next guess is: ",guess)
    elif userinput == "lower":
        highnumber = guess
        guess = random.randrange(lownumber, highnumber)
        print("my next guess is: ",guess)
    elif userinput == "got it":
        print("I guessed right! Your number was",guess)
        won = True 

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1