Number Guess Issue


#1

Trying to complete the number guess portion of learn Python and repeatedly getting an error on step 17. The error is “max_val” is not defined when I try to guess a number higher than the limitations I have programmed. Copied the code below. Any help is greatly appreciated.

"This program rolls a pair of dice and compares the outcome to your guess."
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 number of sides is %d" % max_val
guess=get_user_guess()
if guess > max_val: 
	print "Guess is invalid"
roll_dice(6)

#2

why is this code:

guess=get_user_guess()
if guess > max_val: 
	print "Guess is invalid"

not within the body of the function? max_val is a local variable, it only exists within the function. Trying to access a local variable outside its scope will give an error


#3

Thanks, you are absolutely right. Problem solved by defining the max_val variable as a global variable, thanks again!


#4

that was not the solution i had in mind, why would you do that?

did you see this question:


#5

Yes, though I must have misread it. In any event I did define max_val as a global function and it seems to have solved the problem.


#6

sometimes there are wrong ways to solve a problem, can i see how you implemented the solution? I fear you didn’t solve the problem the right way


#7
"This program rolls a pair of dice and compares the outcome to your guess."
from random import randint
from time import sleep
max_val=0
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 number of sides is %d" % max_val
guess=get_user_guess()
if guess > max_val: 
	print "Guess is invalid"

#8

why would you solve the problem this way? Does the program now work correctly?

you already defined max_val (within the roll_dice function), so i don’t see why you can’t see that you simple got some indent wrong

this:

guess=get_user_guess()
if guess > max_val: 
	print "Guess is invalid"

should be part of the function (body)

add multiple roll_dice function calls, see if your program gives correct output


#9

It did work correctly defining the global function.

Are you just saying print “Guess is invalid” shouldn’t be indented and that would solve the issue?


#10

No, its not working correctly. The code “works”, but the program is not behaving as it should. There is a massive difference right there

no, i am saying rather the opposite. the code which gets the user guess (guess=get_user_guess()) and checks if the guess is valid (if guess > max_val:) should be inside the roll_dice function. I would copy and paste from the instructions, but you didn’t include the project url


#11

I see what you’re saying. I tried to place the guess=get_user_guess() within the reoll_dice function, but now it’s giving me an indentation error: unexpected indent.

Here’s the URL
https://www.codecademy.com/courses/learn-python/projects/number-guess?action=resume_content_item
Step 14 has the instruction to include get_user_guess.

Here’s the code now

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 number of sides is %d" % 	max_val
	guess=get_user_guess()
	if guess > max_val: 
		print "Guess is invalid"

#12

now you added a bit too much indent as you can see:

image

if the editor doesn’t properly reflect this, try to re-indent those lines


#13

The editor doesn’t seem to be reflecting that. I’ve tried re-indenting and seem to get the same error, now for other lines as well.

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 number of sides is %d" % 	max_val
	guess=get_user_guess()
	if guess > max_val: 
		print "Guess is invalid"

#14

the editor uses hard tabs, its not always very pleasant, its not looking good:

image

maybe go to repl.it (or your own editor if you have one set up), and indent the code there, and copy it back into the lesson.


#15

Had a friend copy it in his editor. Copy pasted that and it worked. Thanks for your help.


#16

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