Skip to content

Instantly share code, notes, and snippets.

@hiramf
Created March 1, 2017 04:01
Show Gist options
  • Save hiramf/62f943075285e1fedd7812ed48425746 to your computer and use it in GitHub Desktop.
Save hiramf/62f943075285e1fedd7812ed48425746 to your computer and use it in GitHub Desktop.
Exercise 9: Guessing Game
import random
def new():
x = random.randint(1,9)
return x
def game(x,n,y):
while True:
g = (input("guess: "))
if g == "exit":
print("game over.")
print("Guesses: %d " % n)
print("Games Played: %d" % y)
break
elif int(g) < 1 or int(g) > 9:
print("please enter a number between 1 and 9")
elif int(g) == x:
n+=1
print("you got it!")
print(n)
x=new()
y+=1
print("New number generated!")
elif int(g) > x:
print("high")
n+=1
print(n)
elif int(g) < x:
n+=1
print("low")
print(n)
while True:
n=0
y=0
x = new()
print('''Welcome to the guessing game!\n"
Your job is to guess a number between 1 and 9.\n
When you guessed correctly, a new number will
be generated and you get to play again!\n
Type 'exit' to end the game.
''')
game(x,n,y)
print("Thanks for playing!")
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment