Multiple Errors: Indent, Syntax


#1


I am having trouble with this program.

errors include:
- invalid syntax on line 22
else:
.....^
- multiple indent errors but can't seem to fix them.

I have even copied and pasted other people's code and it is not working for me. Help!


from random import randint
from time import sleep

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 possible value is: " + str(max_val)
  sleep(1)
  user_guess = get_user_guess()
  if user_guess > max_value:
    	print "No guessing higher than the maximum possible value!"
    	return
	else:
  		print "Rolling..."
		sleep(2)
 		print "The first Value is: %d" % first_roll
 		sleep(1)
  		print "The second value is: %d" % second_roll
  		sleep(1)
  		total_roll = first_roll + second_roll
  		print "the total value is: %d" % total_roll
  		if user_guess > total_roll:
    		print "You Won!"
   			return
   		else:
     		print "You lost, try again."
   			return
roll_dice(6)


#2

Code's indentation is wrong.
Indentation is used to separate blocks of code or give structure to a code.
It's key element of Python syntax.
Its actually spaces we give (4 space bars)

The if/else syntax will look like this..

if (condition):
    # Your code
else:
    # Your code

You can see how if and else are at same indentation level.

Also You need to use return keyword correctly! (refer to instruction)


#3

That must of been a pasting error. It shows correct on my screen.


#4

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