I modified NumberGuessProject and it gave me error messages!


#1

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

I have done what the quiz said and it work just fine so I modify it a little. I thought, why not? Here is what I thought.

I know the function int( raw_input() ) allow me to use the number from the user to use in my code. In the original version, I had to call the function roll_dice() in order to run the program and I had to input the number of sides of the dice in the calling code and saved it before I run the program. I thought it would be cool if the user can also decide how many sides of the dice we are going to use in this program so I tried to modify the code so it would receive the input number of sides from the user to run the program before the dice start to roll and before the user can input their guess.

After the modification, the system gave me error messages. I have tried to fix my code many times. Here is the last result of my latest fix. I don’t have any other record of any of my previous attempts trying to fix the code. A few times, the terminal returned "Pick the number of sides: " and after I entered an integer number, the same message appeared over and over again. Sometimes, it successfully run as I want it to, but that happened only once or twice because I changed the code in the middle of before I enter the next number after the "Pick the number of sides: " appear. That times, it broke the loop and work, but once the program end, it ended. Even if I did not change the code, once I started to run “python NumberGuess.py” again, it game me another error messages.

<In what way does your code behave incorrectly? Include ALL error messages.>
ErrorMessageNumberGuess

<What do you expect to happen instead?>

python NumberGuess.py
“Let the game begin!”
Pick the number of sides: user input number of sides
The maximum possible value is: %d
Guess a number: user input number
Rolling…
The first roll is:
The second roll is:
Result…
Awesome! You won!


""" This program would roll a pair of dice, then add the values of the roll. Next, it would ask the user to guess that number. After that it will compare the user's guess to the total value and determine the winner. """
from random import randint
from time import sleep

#get user's raw input
def get_user_guess():
  guess = int(raw_input("Guess a number: "))
  return guess
#get number of sides of the dices from user
def get_number_of_sides():
  number_of_sides = int(raw_input("Pick the number of sides: "))
  return number_of_sides
#create dices
def roll_dice(number_of_sides):
  number_of_sides = get_number_of_sides()
  return 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 is: %d" % max_val
  guess = get_user_guess()
  #
  if guess > max_val:
    print "Invalid Number! No guessing higher than the 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 "Awesome! You won!"
    else:
      print "Haha! You lose!"
#
def guess_number_game():
  print "Let the game begin!"
  get_number_of_sides()
  roll_dice(number_of_sides)
#
guess_number_game()

#2

number_of_sides is a local variable, it only exist within the get_number_of_sides function. To access number_of_sides outside the function you use return number_of_sides, which is very good, however, if we then look at the function call of number_of_sides:

def guess_number_game():
  print "Let the game begin!"
  get_number_of_sides()

you do nothing with the returned result?


#3

Does that mean I did not tell the program what it should do with the input number? If so, what should I do to fix it? I was meant to use it as a calling function. The part guess_number_game() was created to use as a calling function to call both functions, get_number_of_sides and roll_dice .


#4

it means you have a scope problem:

def get_number_of_sides():
  # number_of_sides is local, it only exist within this function
  number_of_sides = int(raw_input("Pick the number of sides: "))
  # which is why you use return here
  return number_of_sides

def guess_number_game():
  print "Let the game begin!"
  # calling the function
  # but you do nothing with the returned result
  get_number_of_sides()
  # so still no access to number_of_sides

Very good design choice, but not relevant to the error you are receiving.

what is it your struggling with? variable scope? Not understanding return sufficient enough?


#5

I think probably I don’t understand both enough.


#6

that is a problem. Then this becomes very challenging.

lets start with scope, look at this code:

x = 3

def example():
   y = 5
   print x, y

print x
# print y

x is global variable, we can access it both inside and outside the function

y on the other hand local variable, it only exist within the function. So when we try to print y inside the function, that goes fine.

however, if you un-comment print y at the end of the script and try again, you get an error. Given y is local is not accessible outside the function

to overcome this problem, we can use return:

def example():
   y = 5
   return y

print example() # output: 5

now we print the returned result. However, for your program, that isn’t good enough, we will need number_of_sides later, so just printing it won’t do, we need to store the returned result in a variable:

def example():
   y = 5
   return y

z = example()
print z # output: 5

thinking about it, this is a concept you are actually very familiar with, storing the returned result in a variable, you do it here:

guess = int(raw_input("Guess a number: "))

and here:

first_roll = randint(1, number_of_sides)
second_roll = randint(1, number_of_sides)

although these are built-in function, they also return some result

hope this makes sense

also, inside your roll_dice function, remove this line:

number_of_sides = get_number_of_sides()

you moved it get_number_of_sides function call to guess_number_game function. Having get_number_of_sides function call inside the guess_number_game function is a better design


#7

It works. Thank you.


#8

not important, do you understand scope and return concepts now? That is far more important then just getting the code to work


#9

I think I understand it better now.


#10

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