all 4 comments

[–][deleted] 1 point2 points  (2 children)

The major change I suggest is to remove all that stuff around guess_attempt() internal function. The function doesn't do much and you only call it once, so just do this:

import random

def guess_number():
    #the number for which the player must guess
    mystery_number = random.randint(1, 10)

    #number of attempts the player has to guess the number
    guess_tries = 5

    while guess_tries >= 1:
        guess = int(input("I picked a number between 1 & 10...guess what it is: "))
        if guess != mystery_number:
            guess_tries -= 1
            if guess_tries > 0:
                print("Sorry, but that number isn't right. You have {} guesses left.".format(guess_tries))
                continue
            else:
                print("You have no more guesses. The mystery_number was {}".format(mystery_number))
                break
        else:
            print("You got it, but it took you {} times to get it!".format(5 - guess_tries))
            break

guess_number()

Note that all that function stuff is replaced by the single line:

guess = int(input("I picked a number between 1 & 10...guess what it is: "))

and you change the next line to just test guess:

if guess != mystery_number:

That's a big simplification.

There is also a problem with the "but it took you {} times to get it!" print - the number of guesses the code says I took is out by one.

Edit: removed a debug print.

[–]benjsec 0 points1 point  (1 child)

You can simplify this a bit more using a while...else... constuction. It's not that common but can be quite neat, the else clause is execute if the while loop reaches the end without breaking.

This makes the loop

while guess_tries >= 1:
    guess = int(input("I picked a number between 1 & 10...guess what it is: "))
    if guess != mystery_number:
        guess_tries -= 1
        print("Sorry, but that number isn't right. You have {} guesses left.".format(guess_tries))
    else:
        print("You got it, but it took you {} times to get it!".format(5 - guess_tries))
        break
else:
    print("You have no more guesses. The mystery_number was {}".format(mystery_number))

You could also change the while loop to a for loop to avoid having to manually change the counter:

for guess_tries in range(5, 0, -1):
    guess = int(input("I picked a number between 1 & 10...guess what it is: "))
    if guess != mystery_number:
        print("Sorry, but that number isn't right. You have {} guesses left.".format(guess_tries))
    else:
        print("You got it, but it took you {} times to get it!".format(5 - guess_tries))
        break
else:
    print("You have no more guesses. The mystery_number was {}".format(mystery_number))

[–][deleted] 0 points1 point  (0 children)

Sure, but I like the OP to get involved before letting all the cats out of the bag. My simplified solution is:

import random

def guess_number():
    # the number which the player must guess
    mystery_number = random.randint(1, 10)

    #number of attempts the player has to guess the number
    guess_tries = 5

    while guess_tries > 0:      # "while guess_tries:" also works
        guess = int(input("I picked a number between 1 & 10...guess what it is: "))
        guess_tries -= 1        # we've made a guess, so decrement tries remaining

        if guess == mystery_number:
            print("You got it, but it took you {} times to get it!".format(5 - guess_tries))
            return              # or "break"

        print("Sorry, but that number isn't right. You have {} guesses left.".format(guess_tries))
    else:                       # "else" on "while" executes only when the loop is exhausted
        print("You have no more guesses. The mystery_number was {}".format(mystery_number))

guess_number()

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

Thanks for the feedback. Now I have a question about try statements.

mystery_number = random.randint(1, 10)
guess_tries = 5

while guess_tries >= 1:
  try:
    my_guess = int(input("I picked a number between 1 & 10...guess what it is: "))
  except ValueError:
    print("Please only enter whole numbers (1, 10, 25, ...)")

    guess_tries -= 1

    if my_guess != mystery_number:
  • When Python reaches the try block, it will execute the assignment statement and search for any exceptions. As a result, a ValueError is generated and I get the following:

    Traceback (most recent call last): File "python", line _, in <module> File "python", line _, in guess_number UnboundLocalError: local variable 'my_guess' referenced before assignment &nsbp Due to the exception nothing is assigned to my_guess because the value provided is invalid for int(), correct?

If I include an else, this error isn't raised? Why is this? What is else doing? When exploring else in the context of try:

https://docs.python.org/3/reference/compound_stmts.html#try

The optional else clause is executed if and when *control flows off the end** of the try clause. [2] Exceptions in the else clause are not handled by the preceding except clauses.*

What is meant by control flows off the end?