Python Python Basics (2015) Number Game App Number Game Refinement

Gilang Ilhami
Gilang Ilhami
12,045 Points

Other framework Does this code only work in Workspace? I tried on another framework but it doesn't work

import random


# safely make an interger
# limit the number of guesses
# too high
# too low
# play again

def game():
    # generate a random number between 1 and 10
    secret_num = random.randint(1, 10)
    guesses = []

    while len(guesses) < 5:
        try:
            # get a number gueast for the player
            guess = int(input("Guest a number between 1 and 10: "))
        except ValueError:
            print("That is not a number")
        else:
            # corporate guest to secret number
            if guess == secret_num:
                print("That's right! {}!".format(secret_num))
                break
            elif guess < secret_num:
                print("Higher than {}".format(guess))
            elif guess > secret_num:
                print("Lower than {}".format(guess))
            else:
                print("That's not it")
            guesses.append(guess)

    else:
        print("You know, it's actually {}.".format(secret_num))
    play_again = input("Play again? Y/n ")
    if play_again.lower() != "n":
        game()
    else:
        print("Game Over")

        # print a hit or a miss


game()

1 Answer

It should work... unless the other framework you're trying it in is Python 2, in which case you'll have to change some syntax for it to work (eg all print statements must not be in parenthesis in Python 2).