0

I have an exerceise from the course I study:

Write a program that picks a random integer from 1 to 100, and has players guess the number. The rules are:

If a player's guess is less than 1 or greater than 100, say "OUT OF BOUNDS" On a player's first turn, if their guess is within 10 of the number, return "WARM!" further than 10 away from the number, return "COLD!" On all subsequent turns, if a guess is closer to the number than the previous guess return "WARMER!" farther from the number than the previous guess, return "COLDER!" When the player's guess equals the number, tell them they've guessed correctly and how many guesses it took!

For some reason, even if the guessing number is correct, it will still ask me to enter a new number. Can you please help me to fix it ?

from random import randint

# picks random integer
raffle = randint(1,100)

# entering first guess
guess = int(input("Guess the random number: "))

# checking first guess
if guess < 1 or guess > 100:
    print("OUT OF BONDS")
else:
    if guess == raffle : 
        print("Correct! You figured the number after the first try!")
    else if guess > raffle:
        if guess-raffle < 11:
            print("WARM!")
        else:
            print("COLD!")
    else:
        if raffle-guess < 11:
            print("WARM!")
        else:
            print("COLD!")

guess_count = 1
match = False

def guess_check():
    next_guess = int(input("Guess the random number again: "))
    if next_guess < 1 or guess > 100:
        print("OUT OF BONDS")
    else:
        if next_guess == raffle : 
            print("Correct! You figured the number!")
            match = True
        elif next_guess > raffle:
            if next_guess-raffle < 11:
                print("WARMER!")
            else:
                print("COLDER!")
        else:
            if raffle-next_guess < 11:
                print("WARMER!")
            else:
                print("COLDER!")

while match != True:
    guess_check()

print(f"The random number is: {raffle}")
```python
Aviran
  • 49
  • 1
  • 4
  • 1
    Common Python problem (and the question is probably a dupe) ... when you assign to `match` within your function, you are creating a new local variable, distinct from your global `match` variable. One way to fix this is to say `global match` at the top of your `guess_check` function; however, using globals like this is generally something to avoid, and it's best to rewrite your script. For now, try the `global` thing. :) – Ray Toal Jul 14 '19 at 06:14
  • 1
    Not a perfect duplicate but this has the answer [Python Global Variable not updating](https://stackoverflow.com/questions/17911831/python-global-variable-not-updating) – Jab Jul 14 '19 at 06:21

3 Answers3

1

The problem is that the assignment to match inside guess_check treat match is a local variable and does not change the "global" match variable.

One solution is to add the following in guess_check before changing the value of match -

global match
match = True

Personally I would rather return the match value from the function rather than using global variable.

See more about global variables here

Tom Ron
  • 5,906
  • 3
  • 22
  • 38
1

Just have your function guess_check() return a boolean, True or False, instead of using the global variable match. Then when you call guess_check() in your while loop you need to reassign match to the value returned by guess_check(). i.e.

def guess_check():
    next_guess = int(input("Guess the random number again: "))
    if next_guess < 1 or guess > 100:
        print("OUT OF BONDS")
    else:
        if next_guess == raffle : 
            print("Correct! You figured the number!")
            return True
        elif next_guess > raffle:
            if next_guess-raffle < 11:
                print("WARMER!")
            else:
                print("COLDER!")
        else:
            if raffle-next_guess < 11:
                print("WARMER!")
            else:
                print("COLDER!")
    return False

while match != True:
    match = guess_check()
Austin Marino
  • 136
  • 1
  • 4
0

As mentioned by @TomRon, you need to add those lines and also, in the last print statement you need to modify is as follows:

print("The random number is: {raffle}".format(raffle=raffle))

So, new code might be look like this:

        if next_guess == raffle:
            print("Correct! You figured the number!")
            global match
            match = True
Yash Khatri
  • 27
  • 1
  • 2