2

So I'm new to python and I tried making a random number game generator as an exercise. You need to guess the random number in a limited number of tries. Here's my code.

import random

rand_num  = random.randint(1,6)

print("\nNUMBER GUESSING GAME")
lives = 5

def guess():
    guess = int(input("Input guess: "))
    return guess

print(rand_num) #to see the correct answer

x = guess()
if x == rand_num:
    print("Good job! YOU WIN.\n")

else:
    while x != rand_num:
        
        if x == rand_num:
            print("Good job! YOU WIN.\n")
            break

        lives -= 1
        print("Incorrect. Please try again")
        guess()

        if lives == 0:
            print("YOU LOSE.\n")
            break

So if your first guess is wrong, and you found the right answer in the subsequent tries, the game won't recognize it.

Thanks in advance.

1 Answer 1

2

You were on the right track. In the future, walk yourself through the program thinking what each line is doing and whether the sequence makes logical sense.

There was only one actual error in your code: you didn't collect a new x value in your code when calling guess in the while loop. The other elements were correct, just out of order. Here is a more logical order, with the collecting issue fixed,

import random

rand_num  = random.randint(1,6)

print("\nNUMBER GUESSING GAME")
lives = 5

def guess():
    guess = int(input("Input guess: "))
    return guess

print(rand_num) #to see the correct answer

x = guess()
if x == rand_num:
    print("Good job! YOU WIN.\n")
else:
    while x != rand_num:
        # 1) Notify the user
        print("Incorrect. Please try again")
        # 2) Decrement remaining guesses
        lives -= 1
        # 3) Check if maximum number of guesses
        # reached - end if yes
        if lives == 0:
            print("YOU LOSE.\n")
            break
        # 4) Get a new guess 
        x = guess()
        # 5) Compare            
        if x == rand_num:
            print("Good job! YOU WIN.\n")

You may want to notify your player what are the ranges of numbers to guess. You can also extend this exercise, and code several difficulty levels, each with a different range (e.g. simple level 1-6, medium 1-20, etc.), to be chosen by the player.


As @Stef mentioned in the comment, it is technically cleaner and more common to include the guess number (lives) condition as part of the while loop condition,

import random

rand_num  = random.randint(1,6)

print("\nNUMBER GUESSING GAME")
lives = 5

def guess():
    guess = int(input("Input guess: "))
    return guess

print(rand_num) #to see the correct answer

x = guess()
if x == rand_num:
    print("Good job! YOU WIN.\n")
else:
    while (x != rand_num) and (lives > 0):
        # 1) Notify the user
        print("Incorrect. Please try again")
        # 2) Decrement remaining guesses
        lives -= 1
        # 3) Get a new guess 
        x = guess()
        # 4) Compare, enf if correct             
        if x == rand_num:
            print("Good job! YOU WIN.\n")

# If ending because no more guesses left 
if lives == 0:
    print("YOU LOSE.\n")

I use brackets for each condition, it is not necessary here and a matter of style. I like to separate conditions this way.

Also, the "YOU LOSE.\n" part is now printed at the end, if the all the guesses were exhausted.

One note: with your code as it is now, your player actually gets to guess 6 times, once in the beginning and 5 more times in the while loop.

Sign up to request clarification or add additional context in comments.

4 Comments

I would write while x!= rand_num and lives > 0 because having a loop condition then breaking on a different condition is deceiving and makes it harder to understand what lives is for.
@Stef I agree, just wanted to stick to the original, but I will add it as a better choice now.
Thanks a lot guys, it works fine now. I'll definitely upgrade it and add things whenever I learn new techniques. The levels is definitely one, also a checker if the input is int or not, etc
You can unindent the second if x == rand_num: so it's tested out of the loop (in fact you can combine it with if lives==0 so it's an if / else)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.