Python, Unit 4 (Functions): Number Guess (Project)

#1

``````Hello everyone!

I am  currently stuck on the Number Guess part at the end of Unit 4. I have written all of the code to the best of my ability, and it runs, but it runs improperly.

Here is my code:

#imported to make 'random dice rolls' possible
from random import randint

#imported to simluate the time that it takes to roll dice
from time import sleep

def get_user_guess():
user_guess = int(raw_input("What is your guess?: "))
return get_user_guess()

def roll_dice(number_of_sides):
first_roll = randint(1, number_of_sides)
second_roll = randint(1, number_of_sides)
max_val = number_of_sides * 2
print "The Max value is: " + str(max_val)
sleep(1)
user_guess = get_user_guess()
if user_guess > max_val:
print "The number that you have chosen is invalid. Program now exiting..."
return
else:
print "Rolling..."
sleep(2)
print "The first value is: %d" % (first_roll)
sleep(1)
print str("The second value is: %d" % (second_roll))
sleep(1)
total_roll = first_roll + second_roll
print total_roll
print "Result..."
sleep(1)
if user_guess > total_roll:
else:
print "You lose."

roll_dice(6)

When I run this code, the console tells me that the max value is "12", and then asks "What is your guess?", but basically, no matter how many numbers I put in, it just keeps asking the same "What is your guess?" Even if I put a number greater than 12, it still asks me again. If I don't put a number in, or if I put a letter in, then the program crashes.

#2

you created a recursive function call:

``````def get_user_guess():
user_guess = int(raw_input("What is your guess?: "))
return get_user_guess()``````

it will endless prompt the user for a number. Are you sure you didn't intent to return user_guess?

#3

Ohhh. That fixed that problem. Thank you very much!