Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # A Number Guessing Game
- # Demonstration script
- import random
- max_tries = 4
- while True:
- secret = random.randint(1, 20)
- print("I am thinking of a number between 1 and 20. Can you guess it?\n")
- for attempt in range(max_tries):
- while True:
- try:
- n = int(input("Enter your guess: "))
- break
- except:
- print("You did not enter a number. Try again\n")
- if n == secret:
- print("Congratulations. You guessed correctly!")
- break
- elif n < secret:
- print("Your guess is too low. ", end="")
- else:
- print("Your guess is too high. ", end="")
- if attempt < (max_tries - 1):
- print("Try again")
- print()
- else:
- print("Bad luck! You ran out of tries.")
- response = input("Play again? (N / Y): ")
- if response[0] not in "Yy":
- break
- print("Thanks for playing!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement