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

Page 1 of 1

## 10 Replies - 2053 Views - Last Post: 10 January 2014 - 07:49 PMRate 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=338030&amp;s=a2b4750b5ebf39d9fa98bbc8e993222d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 kjarva

Reputation: 2
• 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

• Beginner

Reputation: 11224
• Posts: 19,242
• 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.

### #3 kjarva

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

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

Posted 10 January 2014 - 05:36 PM

I can't believe I missed something so stupid! Thanks Jon!

### #4 jon.kiparsky

• Beginner

Reputation: 11224
• Posts: 19,242
• Joined: 19-March 11

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

Posted 10 January 2014 - 05:37 PM

### #5 kjarva

Reputation: 2
• 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?

### #6 jon.kiparsky

• Beginner

Reputation: 11224
• Posts: 19,242
• 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?

### #7 kjarva

Reputation: 2
• 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)
```

### #8 jon.kiparsky

• Beginner

Reputation: 11224
• Posts: 19,242
• 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.

### #9 kjarva

Reputation: 2
• 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

### #10 jon.kiparsky

• Beginner

Reputation: 11224
• Posts: 19,242
• 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.

### #11 kjarva

Reputation: 2
• 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
```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
```