Syntax error for "else" - cannot see where i have it wrong


#1

from random import randint
def get_user_guess():
  user_guess=int(raw_input("Guess a number:"))
  return 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 possibe value is: " + str(max_val)
sleep(1)
user_guess=get_user_guess()
if user_guess>max_val:
  	print "no guess higher than the maximum value is allowed."
return
else:
  print "Rolling..."
  sleep(2)
  print "The first value is: %d" % first_roll
  sleep(1)
  print "The second value is: %d" % second_roll
  total_roll=first_roll+second_roll
  print "Result..."
  if user_guess>total_roll:
    print "You have won"
    return
  else:
    print "You are a loser!!!"
    return
roll_dice(6)


Hello, i am not totally happy with the indenting TBH as some of the later Boolean phrases seem out of the function to me. But anyhow, the 'else' phrase line 16, the precedes the 'print "Rolling..."' line, is coming up as a syntax error. As far as I can make out it looks identical to what is displayed in the hints. The 'return' above it I have actually indented to be in line with the 'print'. Am sure there is an error i am just not seeing.
Thank you for your help in advance.


#2

  	print "no guess higher than the maximum value is allowed."
return

return should be indented so it is inside if


#3

Thank you for the swift reply; I have tried indenting 'return' in numerous ways, I get told that it is indented incorrectly. That the indent does not match any other line.


#4

it should match the indent of the print on the line above, which is indented with a ridiculous amount of spaces

Indent both the print and return with 2 spaces (like the rest of your code)


#5

'''if user_guess>max_val:
print "no guess higher than the maximum value is allowed."
return
else:'''

Is this better? The original spacing was done on pressing return having finished the line above. This says that 'return' is outside the function. It looks in to me.


#7

if user_guess>max_val:
	print "no guess higher than the maximum value is allowed."
	return
else:

#8

can i see your full code?


#9

Amazingly I have figured out what was wrong. I had not kept the indent after the phrase: 'max_val...'
once i did that then it all worked. Amazingly once that was done all my other indent and syntax problems melted away. Thank you for your help, as it helped me focus on indentation generally.


#10