4
\$\begingroup\$
import random
random_number = random.randint(1, 15)
guess = int(input("What do you think my number is? "))
tries = 1

while guess != random_number:
    tries = tries + 1
    retry = int(input("You got it wrong you're welcome to try again: "))
    if retry == random_number:
        print(f"You got it right in {tries} tries ")
        break
if guess == random_number:
    print("You got it right on your first try are you a god?")

So in this one I took into account some of the naming conventions for Python that I was told earlier (although I didn't use functions here as I'm still trying to figure out how to implement them over variables). I also took the time to learn about importing although I'm just barely scratching the surface with the use of .randint() which was quite a challenge for me. I also had a hard time trying to figure out how to stop the while loop from repeating the 8th line with each run of the while loop. I figured it kept repeating because technically guess != random_number so I added a break in there because I thought that once you guessed the right number adding a break would stop the while loop from printing out that first string (although that's probably not why it worked, if you have an answer I'd love to understand why it worked) I also just realized that this entire process could've probably been easier if I had used a function instead of the guess variable, but at least I know now.

edit: I've read some of the things people have been saying, and I really appreciate the help and breakdown, especially for why the break works.

\$\endgroup\$
2
  • 2
    \$\begingroup\$ Anyone playing the game (that has not read the code), will not know that the number will be between 1 and 15. \$\endgroup\$ Commented Mar 2, 2023 at 14:19
  • 1
    \$\begingroup\$ A slight variation of the program would be guiding the user towards the correct guess by telling them whether their guess is lower/higher than the target number when they get it wrong. This would allow you to increase the range (1-100, for example). \$\endgroup\$ Commented Mar 3, 2023 at 11:25

2 Answers 2

10
\$\begingroup\$

The reason you need the break statement is that the variable guess is never modified after the user's first input. If you assigned the user's guesses to the guess variable, then the break wouldn't be needed.

import random
random_number = random.randint(1, 15)
guess = int(input("What do you think my number is? "))
tries = 1

while guess != random_number:
    tries = tries + 1
    guess = int(input("You got it wrong you're welcome to try again: "))
    if guess == random_number:
        print(f"You got it right in {tries} tries ")
if guess == random_number:
    print("You got it right on your first try are you a god?")

Now, the loop condition will stop the loop. However, the if statement after the while loop will always print the message since guess == random_number will always be true at this point. We need to change the if condition. Since the message relates to the user guessing right on the first try, let's use the tries variable.

import random
random_number = random.randint(1, 15)
guess = int(input("What do you think my number is? "))
tries = 1

while guess != random_number:
    tries = tries + 1
    guess = int(input("You got it wrong you're welcome to try again: "))
    if guess == random_number:
        print(f"You got it right in {tries} tries ")
if tries == 1:
    print("You got it right on your first try are you a god?")

About the print(f"You got it right in {tries} tries "): this will only ever be run once, since the condition that causes it to run will immediately terminate the loop. Let's move this outside the loop. This line is only run when the user guesses wrong on the first try, so tries != 1. This is the opposite of the if condition following the while loop, so we can use an else branch here.

import random
random_number = random.randint(1, 15)
guess = int(input("What do you think my number is? "))
tries = 1

while guess != random_number:
    tries = tries + 1
    guess = int(input("You got it wrong you're welcome to try again: "))

if tries == 1:
    print("You got it right on your first try are you a god?")
else:
    print(f"You got it right in {tries} tries ")

Now the program has a nice step-by-step structure:

  1. Create a random number
  2. Get user's first guess
  3. While the user's guess is wrong, keep asking for more guesses
  4. Print a message based on how many guesses the user needed

For a final edit, tries = tries + 1 can also be written like this: tries += 1.

\$\endgroup\$
1
  • \$\begingroup\$ Thank you for providing a more simplified version of my program with tips, it helps a lot. \$\endgroup\$
    – Uncharted
    Commented Mar 2, 2023 at 23:16
5
\$\begingroup\$

Handling input well is hard:

guess = int(input("What do you think my number is? "))

Try this a few times, and see what happens if you enter something that's not a number: the int() conversion throws a ValueError, and because we don't catch it, this kills the whole program.

We can improve this, by creating a function that will read an integer, and ask the user to retry if anything else is supplied:

def int_input(prompt):
    while True:
        try:
            return int(input(prompt))
        except ValueError:
            print("An integer value is required")

Now we can write

guess = int_input("What do you think my number is? ")
\$\endgroup\$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.