Python Number Guess


#1

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"
  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 "The total roll is:%d" % total_roll
    print "Result...." 
    sleep(1)
    if guess > total_roll:
      print "You Won..!!"
      else:
        print "You Lost"
        
roll_dice(6)

#2

you do it correctly here:

  if max_value < guess:
    print "Invalid"
  else:

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.


#3

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?


#4

check the spelling of your variable names


#5

Thank you it works now. :smile:


#6

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