Created
July 31, 2017 15:18
-
-
Save jgarciabu/9b18aaa681efbcb5570b884377843a6d to your computer and use it in GitHub Desktop.
Random Number Game Ex 8
This file contains 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
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
""" | |
Created on Fri Jul 28 13:11:24 2017 | |
@author: Jeff Garcia | |
Generate a random number between 1 and 9 (including 1 and 9). Ask the user to | |
guess the number, then tell them whether they guessed too low, too high, or | |
exactly right. (Hint: remember to use the user input lessons from the very | |
first exercise) | |
Extras: | |
Keep the game going until the user types “exit” | |
Keep track of how many guesses the user has taken, and when the game ends, | |
print this out. | |
""" | |
import random | |
import sys | |
rand = random.randint(1,9) | |
totalguesses = 0 | |
number = print('Please guess a number between 1 and 9.') | |
number = input() | |
if number == 'exit': | |
print('Thanks for playing!') | |
sys.exit() | |
number = int(number) | |
if number > rand: | |
print('Your guess is too high.') | |
if number < rand: | |
print('Your guess is too low.') | |
while number != rand: | |
totalguesses += 1 | |
number = print('Try again. Guess a number between 1 and 9.') | |
if number == 'exit': | |
print('Thanks for playing!') | |
sys.exit() | |
number = int(input()) | |
if number > rand: | |
print('Your guess is too high.') | |
if number < rand: | |
print('Your guess is too low.') | |
if number == rand: | |
break | |
if number == rand: | |
totalguesses += 1 | |
totalguesses = str(totalguesses) | |
print('Congratulations! It took you ' + totalguesses + ' guesses to win!') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment