Skip to content

Instantly share code, notes, and snippets.

@libert-xyz
Created January 5, 2016 16:52
Show Gist options
  • Save libert-xyz/be109a4703b36af815ff to your computer and use it in GitHub Desktop.
Save libert-xyz/be109a4703b36af815ff to your computer and use it in GitHub Desktop.
#Libert R Schmidt
#[email protected]
#Practice Python http://www.practicepython.org/exercise/2014/04/02/09-guessing-game-one.html
import random
def start_game():
num = int(input("Guess the number between 1 and 9 --> "))
game(num)
def game(num):
attempt = 1
keep = True
randnum = random_number()
while num > 0:
if num > randnum:
print("**Too high!!, keep trying or type exit to exit the Game**")
attempt = attempt + 1
num = keep_trying()
if num < randnum:
print("**Too low!!, keep trying or type exit to exit the Game**")
attempt = attempt + 1
num = keep_trying()
if num == randnum:
print("**You won!, %d was the number**" %randnum)
print("Number of attempts %d" %attempt)
num = -1
def keep_trying():
new_num = str(input("1-9 or exit --> "))
if new_num == 'exit':
new_num = -1
return (new_num)
else:
return int(new_num)
def random_number():
randnum = random.randint(1,9)
return randnum
start_game()
@mprat
Copy link

mprat commented Mar 18, 2016

Nicely done!

@mihneanichifor
Copy link

Nice!
but what does the keep = True do in the game(num) func? was the while num> 0 not enough?

thanks!

@armanperalta
Copy link

Add an error handling to complete this code.
It crashes when I enter without a value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment