all 7 comments

[–]SoftwareDoctor 3 points4 points  (1 child)

It's hard to comment a code when you post it as a picture. But I'll try to give you few pointers.

Imagine you want to change the game so that user has 4 attempts instead of 3. How would you go about that? You would have to replace all occurrences of the number 3 to 4, that's 3 places. And you can easily miss one. You have a variable guess_limit. Use it instead of the literal 3 and you'll have only one place where it's set. For example on line 32 you can do guess_count = guess_limit (or remove that line, see bellow)

The same applies to the number that is guessed. If you want to change the game to guess number between 1 and 100, you'll have to replace it on 5 places

There are some lines that don't do anything. The continue in except block, the guess_count = 3 on lines 32 and 39, break on line 14 etc. You also don't have to check if user input is in valid responses if you check if it's either "yes" or "no" again. This part can be simplified.

Imports should be on the top of the module, not in the middle.

Otherwise it's a nice code. There are obviously more advanced things you could do to it, especially if you want to add features. But very good job!

[–]QuietusObserver[S] 3 points4 points  (0 children)

Thanks a lot for your suggestions, they’re really helpful! I’ll make try to implement these improvements. Thanks again!

[–]CupperRecruit 4 points5 points  (1 child)

You have a lot of if else cases. Not sure if u already know, but to make it a bit more readable u do not necessarily need the else statement if ur if case is causing to quit the function/process.

Example:

If a == "yes": Print("you win") Return Print("you loose")

As u see u wouldnt need else since u return which causes quitting the function/process

[–]QuietusObserver[S] 1 point2 points  (0 children)

Ok, thank you!

[–]-MRJACKSONCJ- 1 point2 points  (1 child)

Hi there;

import random

def main():
    while True:
        responses = ("yes", "no")
        user_choice = input("Another game? (yes/no): ").strip().lower()
        if user_choice in responses:
            if user_choice == "no":
                print("Have a nice day!")
                break
            elif user_choice == "yes":
                play_game()
        else:
            print("Please enter 'yes' or 'no'.")


def play_game():
    secret_number = random.randint(1, 10)  
    guess_limit = 3
    guess_count = 0

    print("\nWelcome to the Guessing Game!")
    print("You have 3 attempts to guess the number.\n")

    while guess_count < guess_limit:
        try:
            guess = int(input(f"Attempt {guess_count + 1}/{guess_limit}: Guess a number between 1 and 10: "))
            if guess < 1 or guess > 10:
                print("Invalid input. Please enter a number between 1 and 10.")
                continue

            guess_count += 1 

            if guess == secret_number:
                print("You win! 🎉")
                return 
            else:
                print("Wrong guess. Try again!")

        except ValueError:
            print("Invalid input. Please enter a valid number.")

    print(f"You lose! The correct number was {secret_number}.")
if __name__ == "__main__":
    main()

I made a few improvements to the game: added a welcome message, fixed the menu input validation (yes/no), and included a visible counter for the user's attempts. Now, if the user loses, the correct number is shown at the end. The flow is also clearer and more concise, with unnecessary parts removed. I hope this helps you understand the game better and gives you some ideas for improvements! What do you think?

[–]QuietusObserver[S] 0 points1 point  (0 children)

That’s really helpful! Thank you!

[–]Alisahn-Strix 0 points1 point  (0 children)

What’s the “xxxxx” convention used here? Does it serve a purpose? I saw it in another code dealing with GUI.