Last active
April 29, 2017 11:06
-
-
Save odanga94/0bb339b3b5db1eed5680e8a11222f940 to your computer and use it in GitHub Desktop.
CodeAcademy Lesson: Number Guess
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
"""This program randomly rolls a pair of dice and adds the values of the roll. | |
It then asks the user to guess a number, compares the user's guess to the total value and finally decides whether the winner is the user or the program and informs the user who the winner is""" | |
from random import randint | |
from time import sleep | |
def get_user_guess(): | |
user_guess = int(raw_input("Enter your guess:")) | |
return user_guess | |
def roll_dice(number_of_sides): | |
first_roll = randint(1, number_of_sides) | |
second_roll = randint(2, number_of_sides) | |
max_val = number_of_sides * 2 | |
print "The maximum possible value is %s" %(max_val) | |
sleep(1) | |
user_guess = get_user_guess() | |
if user_guess > max_val: | |
print("No guessing higher than the maximum possible value!") | |
return | |
else: | |
print("Rolling...") | |
sleep(2) | |
print("first_roll: %d") %(first_roll) # %d is used to format integers whereas %s formats string variables | |
sleep(1) | |
print("second roll: %d") %(second_roll) | |
total_roll = first_roll + second_roll | |
print("total_roll: %d") %(total_roll) | |
print("Result...") | |
sleep(1) | |
if user_guess > total_roll: | |
print "You Won!" | |
return | |
else: | |
print ("You Lost :(") | |
roll_dice(6) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment