-2

This is the code. Somebody please help. I'm confused. The guess variable is the number of guesses that remain after each guess. I've tried global and it still doesn't work. I'm lost at this point. Please help.

import random 

# WELCOMING USER
print(" WELCOME TO THE NUMBER GUESSING GAME !! ")
print("The number you'll be guessing is between the range 1 - 100 ")

# SELECTING THE CORRECT GUESS RANDOMLY
correct_number = random.randint(1,100)

# ASKING THE USER FOR PREFERED DIFFICULTY AND SETTING NO OF GUESSES 
difficulty_mode  = input("Do you want the game to be easy or hard ? : ").lower()
guess = 0
#testing purposes
print(correct_number)

if difficulty_mode  == "hard":
    guess = 5 
    print("You have 5 guesses")
elif difficulty_mode == "easy" :
    guess = 10
    print(f"You have {guess} guesses")
else:
    print("Wrong input")

# CREATING A FUNCTION TO REDUCE NO OF GUESSES 
def reducing_guesses () :
    global guess
    guess -= 1
    print(f"You have {guess} no. of guesses remaining ")

# CREATING A WHILE LOOP TO RUN GAME 

isTrue = True 
loop_rounds = 0
# ACTUAL WHILE LOOP
while isTrue  == True :
    if guess == 0:
        isTrue = False
        winning_or_loose = 0
    if loop_rounds > 0 :
        user_guess = int(input("Guess again : "))
        if user_guess  == correct_number :
            isTrue == False
            print(f"You win! You guessed:{user_guess} and it was correct ")
            winning_or_loose = 1
        else:
            reducing_guesses()
    if loop_rounds  == 0 :
        user_guess = int(input("Make a guess :"))
        if user_guess == correct_number :
            isTrue  = False 
            print(f"You win! You guessed:{user_guess} and it was correct ")
            winning_or_loose = 1 
        else:
            reducing_guesses()
OneMadGypsy
  • 4,640
  • 3
  • 10
  • 26
David
  • 1
  • 1
  • 1
    Your question suggests that you are finding `guess` is undefined, but when I run your code, I see that `user_guess` is undefined. – Shane Bishop Oct 22 '22 at 14:56
  • I've never had to fix EVERY LINE of someone's post. Please be more diligent when posting your questions. – OneMadGypsy Oct 22 '22 at 15:01
  • add `user_guess = 0` somewhere before your `while` loop – OneMadGypsy Oct 22 '22 at 15:11
  • [How to step through Python code to help debug issues?](https://stackoverflow.com/questions/4929251/how-to-step-through-python-code-to-help-debug-issues) If you are using an IDE **now** is a good time to learn its debugging features Or the built-in [Python debugger](https://docs.python.org/3/library/pdb.html). Printing *stuff* at strategic points in your program can help you trace what is or isn't happening. [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – wwii Oct 22 '22 at 16:16
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Oct 23 '22 at 14:42

1 Answers1

0

All you have to do is put user_guess = 0 and winning_or_loose = 0 somewhere before your while loop, and it fixes your game. Your main issue seems to be that you keep trying to use variables that you never created.

However, you code has a lot of redundancy, and is not organized very well. You have no system in place to handle the user entering bad data. There is no way to play your game a second time, without restarting. Consider the following version of your game. I provide it to you as a somewhat optimized example. It is not perfect. It illustrates how to get rid of about half of your code for the same (or better) results.

You may want to incorporate a system that tells the user if the correct_number is higher or lower than their guess. Currently, your game is practically impossible to win. It's like trying to play a 3D shooter that has no lights, at all. This suggestion is not implemented in the below code.

import random 

#GAME LOOP
while True:
    # WELCOMING USER
    print(" WELCOME TO THE NUMBER GUESSING GAME !! ")
    print("The number you'll be guessing is in the range 1 - 100 ")
    
    #ALL VARIABLES
    won            = 0
    guess          = 0
    user_guess     = 0
    loop_rounds    = 0
    msg            = ('Make a guess : ', 'Guess again : ')
    correct_number = random.randint(1,100)
    
    # ASKING THE USER FOR PREFERED DIFFICULTY AND SETTING NO OF GUESSES 
    while (in_ := input("Do you want the game to be easy or hard ? : ").lower()) not in ('easy','hard'):
        print("Wrong input")
        
    guess = (10,5)[in_=='hard']
    
    #ROUND LOOP
    while guess>0:
        print(f"You have {guess} no. of guesses remaining ")
        
        while not (in_ := input(msg[loop_rounds>0])).isdigit():
            print('Input a number between 1 and 100')
            
        user_guess = int(in_)
        
        if user_guess == correct_number :
            print(f"You win! You guessed:{user_guess} and it was correct ")
            won += 1 
            break
            
        guess -= 1
        loop_rounds += 1
           
    #LOST           
    if user_guess != correct_number:
        print('You failed to guess the number.')
        
    #PLAY AGAIN
    if input('Try again(y,n)? ') == 'n':
        break
OneMadGypsy
  • 4,640
  • 3
  • 10
  • 26