Created
January 5, 2016 16:52
-
-
Save libert-xyz/be109a4703b36af815ff to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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() |
Nice!
but what does the keep = True do in the game(num) func? was the while num> 0 not enough?
thanks!
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
Nicely done!