Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. #Exercise 9
  2.  
  3. #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)
  4.  
  5. #Extras:
  6.  
  7. #1. Keep the game going until the user types β€œexit”
  8. #2. Keep track of how many guesses the user has taken, and when the game ends, print this out.
  9.  
  10. import random
  11.  
  12. num = random.randint(1, 9)
  13. playgame = True
  14.  
  15. print("Welcome to Guess Number Game!\n")
  16.  
  17. while playgame == True :
  18.  
  19. attempt = 0
  20. guess = 0
  21.  
  22. while guess != num :
  23.  
  24. guess = int(input("Guess the Number (1-9) : "))
  25.  
  26. if guess == num :
  27. attempt += 1
  28. print("\nCorrect! the number is " + str(num))
  29. print("attempt : " + str(attempt) + "\n")
  30. continue
  31. elif guess < num and guess > 0 :
  32. print("Too Low.\n")
  33. attempt += 1
  34. elif guess > num and guess < 10 :
  35. print("Too High.\n")
  36. attempt += 1
  37. elif guess < 1 or guess > 9 :
  38. print("Please guess 1-9 only.\n")
  39.  
  40. exit = str(input("exit game(y/n)?\n"))
  41.  
  42. if exit == "y" :
  43. playgame = False
  44. print("\nThanks for playing. -Ihsan Nugraha\n")
  45. elif exit == "n" :
  46. print("\nGood Luck.\n")
  47. else :
  48. print("\nWrong input. I guess you want to play again. Good Luck.\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement