Advertisement
Guest User

GuessGamev2

a guest
Nov 17th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.38 KB | None | 0 0
  1. # This is a guess a number game.
  2.  
  3. import random
  4.  
  5. print("Hello! What is your name?")
  6. userName = input()
  7. print("Nice to meet you, " + userName + ". Let's play a guess a number game, shall we?")
  8.  
  9.  
  10. # Write a function of guessing game
  11. def startsgame():
  12.  
  13.     secretNumber = random.randint(1, 20)
  14.     print("I am thinking of a number between 1 and 20")
  15.  
  16.     # Ask the player to guess 5 times
  17.  
  18.     for guessTaken in range(1, 6):
  19.         print("Take a guess:")
  20.         try:
  21.             guess = int(input())
  22.             if guess > secretNumber:
  23.                 print("Your guess is too high!")
  24.             elif guess < secretNumber:
  25.                 print("You guess is too low!")
  26.             else:
  27.                 break  # Stop the loop if guess correctly
  28.         except ValueError:
  29.             print("Invalid input. Please try again!")
  30.  
  31.     if guess == secretNumber:
  32.         print("Congratulation, " + userName + ". You guess correctly in " + str(guessTaken) + " times!")
  33.     else:
  34.         print("Out of guess. The number I was thinking of was " + str(secretNumber))
  35.  
  36.  
  37. startsgame()
  38.  
  39. # Asking player to continue or to quit
  40. userInput = ""
  41. while not (userInput == "Y" or userInput == "N"):
  42.     print("Do you want to continue? Press Y to continue or N to exit!")
  43.     userInput = input().upper()
  44.     if userInput == "Y":
  45.         startsgame()
  46.     elif userInput == "N":
  47.         break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement