Python Number Guess


This is my code. It shows “Invalid Syntax” for last “else”. What should I do?

from random import randint
from time import sleep

def get_user_guess():
  guess = int(raw_input("Take a guess:"))
  return guess
def roll_dice(number_of_sides):
  x = randint(1, number_of_sides)
  first_roll = x
  y = randint(1, number_of_sides)
  second_roll = y
  max_val = number_of_sides * 2
  print "Max value:%d" % max_val
  guess = get_user_guess()
  if max_value < guess:
    print "Invalid"
    print "Rolling..."
    print "The first roll is:%d" % first_roll
    print "The second roll is:%d" % second_roll
    total_roll = first_roll + second_roll
    print "The total roll is:%d" % total_roll
    print "Result...." 
    if guess > total_roll:
      print "You Won..!!"
        print "You Lost"


you do it correctly here:

  if max_value < guess:
    print "Invalid"

if something do something, else do something else. The else is outside/after the if (by using correct indent), yet later you don’t do this.


Thanks for that and I corrected the error but now when I try to run the code it shows me “max_value is not defined”. What should I do?


check the spelling of your variable names


Thank you it works now. :smile:


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