NumberGuess: NameError not defined


#1



I get the error:
NameError: name "number_of_sides" is not defined
on line "max_val = number_of_sides * 2

Can someone look at my code and help?


"""
Program is a game against the computer"""
from random import randint
from time import sleep
def get_user_guess():
  user_guess = int(raw_input("put a guess"))
  return 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 "Invalid guess"
else:
    print "Rolling..."
    sleep(2)
    print "Your roll is " + first_roll
    sleep(1)
    print "Your second roll is " + second_roll
    sleep(1)
    total_roll = first_roll + second_roll
    print "Result..."
    sleep(1)
    if user_guess>total_roll:
      print "congrats, you won"
    else:
        "you lose"
roll_dice(6)


#2

You have a few errors... Check your indentation.


#3

Okay, I think I fixed it, but I still get the same error:

"""
Program is a game against the computer"""
from random import randint
from time import sleep
def get_user_guess():
  user_guess = int(raw_input("put a guess"))
  return user_guess
def roll_dice(number_of_sides):
  first_roll = randint(1, number_of_sides)
  second_roll = randint(1, number_of_sides)
  return
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 "Invalid"
else:
  print "Rolling..."
  sleep(2)
  print "Your roll is " + first_roll
  sleep(1)
  print "Your second roll is " + second_roll
  sleep(1)
  total_roll = first_roll + second_roll
  print "Result..."
  sleep(1)
  if user_guess>total_roll:
  	print "congrats, you won"
  else:
    "you lose"
roll_dice(6)

#4

Your indentation is still off. The way it is now, everything after your return statement is outside of the function. So the two relevant questions are, 1) where do the instructions say that code should be (#10), and 2) does that return statement belong there?


#5

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