Guest User

Untitled

a guest
Jul 21st, 2018
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. # 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)
  2. # Keep the game going until the user types “exit”
  3. # Keep track of how many guesses the user has taken, and when the game ends, print this out.
  4.  
  5. guess = input("Guess a number between 1 and 9: ")
  6. if isinstance(guess, int):
  7. guess_num = int(guess)
  8.  
  9. times = 0
  10.  
  11. while guess != "exit":
  12. import random
  13. num = random.randint(1,9)
  14. print("Random number is " + str(num))
  15. if guess_num == num:
  16. print("You guessed right.")
  17. elif guess_num < num:
  18. print("You guessed low.")
  19. else:
  20. print("You guessed high.")
  21. guess = input("Guess a number between 1 and 9: ")
  22. if isinstance(guess, int):
  23. guess_num = int(guess)
  24. times += 1
  25.  
  26. print("You guessed " + str(times) + " times")
Add Comment
Please, Sign In to add comment