PaningningPH
PaningningPH

Reputation: 59

Proper way of writing recurring function in python

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.

Upvotes: 2

Views: 57

Answers (1)

atru
atru

Reputation: 4744

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.

Upvotes: 2

Related Questions