Python NumberGuess.py Game - Help needed

https://www.codecademy.com/courses/learn-python/projects/number-guess
“”"
“”“This program will select one winner, either the user or the computer. The user wins if they correctly guess the total of the number rolled on the dice; otherwise, the computer wins!”""

from random import randint

from time import sleep

def get_user_guess():
guess = int(raw_input("Guess a number: "))
return 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 maximum possible value of any roll is: %d” % max_val

get_user_guess(guess)

if guess > max_val:
print “Invalid input! No guessing higher than maximum possible value!”

else:
print “Rolling …”
sleep(2)
print “The first roll is: %d” % first_roll
sleep(1)
print “The second roll is: %d” % second_roll
sleep(1)

total_roll = first_roll + second_roll

print “Result …”
sleep(1)

if guess == total_roll:
print “Congratulations! You won!”

else:
print “Sorry, you lost! Try Again”

roll_dice(6)

“”"
The error message is as below:
$ python NumberGuess.py
Traceback (most recent call last):
File “NumberGuess.py”, line 31, in
total_roll = first_roll + second_roll
NameError: name ‘first_roll’ is not define
d
Could someone please point out why this code is failing as to my mind, I had earlier defined the first_roll variable. Many thanks in advance for your help.

When you ask a question, don’t forget to include a link to the exercise or project you’re dealing with!

If you want to have the best chances of getting a useful answer quickly, make sure you follow our guidelines about how to ask a good question. That way you’ll be helping everyone – helping people to answer your question and helping others who are stuck to find the question and answer! :slight_smile:

Hi @ijemusne,

Could you please format your code according to this topic?

1 Like

@ijemusne It may be because the variable first_roll is a local variable in the function get_user_guess. You may have to globalise the variable, by using the global keyword. But as @stevencopeland said, for any answer to be completely accurate, it is best to format your code. To do so, you can just press this:

.

1 Like