Created
August 2, 2016 00:05
-
-
Save nathantowell/4e65b4119aa8ee9d32f4a4ab6e01e796 to your computer and use it in GitHub Desktop.
A simple game to guess a random number!
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
from random import randint | |
def game(): | |
print("Welcome to the number guessing game!") | |
while True: | |
pair = setup() | |
rand = generate(pair) | |
print("Time to guess the number chosen...") | |
tries = get_guesses(pair, rand) | |
wording = "" | |
if tries == 1: | |
wording = "try" | |
else: | |
wording = "tries" | |
print("You guessed the number in " + str(tries) + " " + wording + "!") | |
print("The number was " + str(rand) + " and there were " + str((pair[1]-pair[0])+1) + " possible answers!") | |
again = input("Play again? [y/n]:") | |
if again == "y": | |
continue | |
else: | |
print("Goodbye") | |
return | |
def setup(): | |
print("Please enter the variables...") | |
return get_values() | |
def get_guesses(pair, rand): | |
print("Enter a number from "+ str(pair[0]) + " to " + str(pair[1]) + "!") | |
correct = False | |
tries = 0 | |
while not correct: | |
tries += 1 | |
guess = int(input("> ")) | |
if guess < pair[0] or guess > pair[1]: | |
print("Try to guess within the range!") | |
if guess == rand: | |
correct = True | |
else: | |
print("Try again!") | |
return tries | |
def get_values(): | |
lower = -1 | |
upper = -1 | |
while lower == -1: | |
lower = int(input("Enter the lowest number: ")) | |
if not lower >= 0: | |
print("You must enter a positive number!") | |
continue | |
while upper == -1: | |
upper = int(input("Enter the highest number: ")) | |
if not upper >= 0: | |
print("You must enter a positive number!") | |
continue | |
if not lower < upper: | |
print("The lower value must be less than the upper value.") | |
return get_values() | |
return [lower, upper] | |
def generate(pair): | |
rand = randint(pair[0], pair[1]) | |
return rand | |
game() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment