Dice roll - issues with the max_val variable

Hi,

I am a bit stuck on the roll dice project. I even adapted the code based on the video explanation but I still get the answer that when I input a number less than the max_val, I get the error that is it higher than the max_val. Could you please help me find a way out? :slight_smile: Here is my code:


""" Simple game of rolling dice and the user guessing the number
"""
from random import randint
from time import sleep

def get_user_guess():
  guess = int(raw_input("Guess a number: "))
  return get_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 maximum possible value is %d." % max_val
  
  guess = get_user_guess()
  if guess > max_val:
    print "Your guess is invalid - too high."
  else:
    print "Rolling..."
    sleep(2)
    print "The first roll gives %d" % first_roll
    sleep(1)
    print "The second roll gives %d" % second_roll
    sleep(1)
    total = first_roll + second_roll
    print "Result..." + str(total)
    sleep(1)
    if total == guess:
      print "You won!"
    else:
      print "You lost, try again."

roll_dice(6)

For comparisons that don’t work the way you expect (or anything really) a little debugging even with something simple like print can be really helpful. It’s easy to view certain programs as a bit of black box but things will likely be much easier if you can open it up and poke around. A little bit of debugging is worth a lot and it’s a skill I think is worth practising.

If you’re comparing A and B but the result in unexpected then check that A and B are what you expect, what’s their value, what’s their type? See if you can trace the issue backwards from the point that doesn’t work the way you expect.

Where to look if you can't trace the issue backwards

At the moment I don’t think your function is return what you expect.

1 Like