Skip to content

Instantly share code, notes, and snippets.

@RandomResourceWeb
Created July 17, 2016 08:49
Show Gist options
  • Select an option

  • Save RandomResourceWeb/a8e3a231332bcb431a36ca374485d487 to your computer and use it in GitHub Desktop.

Select an option

Save RandomResourceWeb/a8e3a231332bcb431a36ca374485d487 to your computer and use it in GitHub Desktop.
Guess the number in Python
#-----------------------------------
# Random Resource Web ©
# Randomresourceweb.blogspot.com
#-----------------------------------
import random
def randnumber():
global number
number = random.randrange(1,101)
def start():
global life
global guesses
global guessed
guesses = 10
guessed = 0
print("Guess the number")
print("You have 10 lives")
randnumber()
start()
while guessed == 0:
guess = input("Number: ")
try:
if int(guess) == number:
print("You are correct! You won with " + str(guesses) + " guesses left and the number was: " + str(number))
again = input("Type Y if you want to play again: ")
if again == "y" or again == "Y":
start()
else:
guessed = 1
elif int(guess) != int(number):
print("Try again")
guesses = guesses - 1
if guesses == 1:
print("You have " + str(guesses) + " guess left")
else:
print("you have " + str(guesses) + " guesses left")
if int(guess) > number:
print("Your guess is too high!")
else:
print("Your guess is too low!")
if guesses <= 0:
print("You lost!\nThe number was: "+ str(number))
again = input("Type Y if you want to play again: ")
if again == "y" or again == "Y":
start()
else:
break
except ValueError:
print("That's not an int!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment