Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python. Variable in while loop not updating.

Tags:

python

I'm very new to programming and I've encountered a problem with a basic guessing game I've been writing. x is a random number generated by the computer. The program is supposed to compare the absolute value of (previous_guess - x) and the new guess minus x and tell the user if their new guess is closer or further away.

But the variable previous_guess isn't updating with the new value. Any help would be appreciated.

Here is the code so far:

    ###Guessing Game
import random

n = 100
x = random.randint(1,n)
print("I'm thinking of a number between 1 and ", n)

##print(x) ## testing/cheating.
count = 0

while True:
    previous_guess = 0 # Should update with old guess to be compared with new guess
    guess = int(input("Guess the number, or enter number greater that %d to quit." % n))
    count += 1

    print(previous_guess)
    print("Guesses: ", count)

    if guess > n:
        print("Goodbye.")
        break

    elif count < 2 and guess != x: 
        print("Unlucky.")
        previous_guess = guess #####

    elif count >= 2 and guess != x:
        if abs(guess - x) < abs(previous_guess - x):
            previous_guess = guess #####

            print("Getting warmer...")
        else:
            previous_guess = guess #####
            print("Getting colder...")

    elif guess == x:
        print("You win! %d is correct! Guessed in %d attempt(s)." % (x,count))
        break 
like image 604
noobish_noob Avatar asked Jun 21 '18 19:06

noobish_noob


People also ask

What happens if the test variable for a while loop is not updated?

You are only printing the output of test() , not updating the variable a so this will always be 0 at each iteration of your loop. when you call test(a) you just print the result, a is never updated - i.e. it stays 0, you just throw away the value returned.

Can you update a variable inside a while loop?

Unlike if statements, the condition in a while loop must eventually become False. If this doesn't happen, the while loop will keep going forever! The best way to make the condition change from True to False is to use a variable as part of the Boolean expression. We can then change the variable inside the while loop.

Why is my while loop not working in Python?

The while loop is not run because the condition is not met. After the running the for loop the value of variable i is 5, which is greater than three. To fix this you should reassign the value before running the while loop (simply add var i=1; between the for loop and the while loop).

Can you update a variable in Python?

Before you can update a variable, you have to initialize it, usually with a simple assignment. In the above example, x was initialized to 6. Updating a variable by adding something to it is called an increment; subtracting is called a decrement.


2 Answers

Your previous guess is being reinitialized every time you loop. This is a very common error in programming so it's not just you!

Change it to:

previous_guess = 0
while True:
   #Rest of code follows

Things you should be thinking about when stuff like this shows up.

  1. Where is your variable declared?
  2. Where is your variable initialized?
  3. Where is your variable being used?

If you are unfamiliar with those terms it's okay! Look em up! As a programmer you HAVE to get good at googling or searching documentation (or asking things on stack overflow, which it would appear you have figured out).

Something else that is critical to coding things that work is learning how to debug.

Google "python debug tutorial", find one that makes sense (make sure that you can actually follow the tutorial) and off you go.

like image 101
bathMarm0t Avatar answered Oct 19 '22 14:10

bathMarm0t


You're resetting previous_guess to 0 every time the loop begins again, hence throwing away the actual previous guess. Instead, you want:

previous_guess = 0
while True:
    guess = ....
like image 42
scnerd Avatar answered Oct 19 '22 13:10

scnerd