Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Generate a random number between 1 and 9 (including 1 and 9). Ask the user to guess the number, then tell them whether they guessed too low, too high, or exactly right. (Hint: remember to use the user input lessons from the very first exercise)
- # Keep the game going until the user types “exit”
- # Keep track of how many guesses the user has taken, and when the game ends, print this out.
- guess = input("Guess a number between 1 and 9: ")
- if isinstance(guess, int):
- guess_num = int(guess)
- times = 0
- while guess != "exit":
- import random
- num = random.randint(1,9)
- print("Random number is " + str(num))
- if guess_num == num:
- print("You guessed right.")
- elif guess_num < num:
- print("You guessed low.")
- else:
- print("You guessed high.")
- guess = input("Guess a number between 1 and 9: ")
- if isinstance(guess, int):
- guess_num = int(guess)
- times += 1
- print("You guessed " + str(times) + " times")
Add Comment
Please, Sign In to add comment