Syntax Error?


#1


I have imported the 'random' library with the function of randint. Is it saying that it isnt a recognized function?

Traceback (most recent call last):
  File "NumberGuess.py", line 42, in <module>
    roll_dice(6)
  File "NumberGuess.py", line 18, in roll_dice
    first_roll = random.randint(1, number_of_sides)
NameError: global name 'random' is not defined


I expect for the code to run without errors. I have written the code to the best of my ability. Thank you for taking the time to look at the code and help out!

"""This program will:

1. Randomly roll a pair of dice
2. Add the values of the roll
3. Ask the user to guess a number
4. Compare the user's guess to the total value
5. Decide a winner (the user or the program)
5. Inform the user who the winner is
"""

from random import randint
from time import sleep

def get_user_guess():
    user_guess = int(raw_input("Guess a number"))
    return user_guess
def roll_dice(number_of_sides):
    first_roll = random.randint(1, number_of_sides)
    second_roll = random.randint(1, number_of_sides)
    max_val = number_of_sides * 2
    print 'The maximum integer is' + str(max_val)
    sleep(1)
    get_user_guess()
    if user_guess > max_value:
        print "No guessing higher than the maximum possible value!"
       	return
    else:
        print 'Rolling...'
        sleep(2)
        print 'The first roll is %d' % (first_roll)
        print 'The second roll is %d' % (second_roll)
        sleep(1)
        total_roll = second_roll + first_roll
        print 'Result...'
        print 'The result is %d' % (total_roll)
        if user_guess > total_roll:
      	    print 'You Won!'
      	    return
        else:
      	    print 'You Lost :('
      	    return
roll_dice(6)


#2

... which means there is a randint function in the namespace.

first_roll = randint(1, number_of_sides)

#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.