IndentationError: unexpected indent


#1



I am going crazy trying different indents and everything I have tried causes another error. Here is my current dilemma, please help me understand what I'm missing.

File "NumberGuess.py", line 14                              
    print "Maxium Value is " + str(max_value)                 
    ^                                                         
IndentationError: unexpected indent          

"""Rolls a pair of dice and player guesses the number, then the computer tries to guess. Closest wins."""

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 "Maxium Value is " + str(max_value)
 		sleep(1)
 		user_guess = get_user_guess()
  if user_guess > max_val:
			print "User guess is higher than Maxium Roll!"
  		return
  else:
  		print 'Rolling...'
  	  sleep(2)
  		print 'The first roll value is: %d' % first_roll
  		sleep(1)
  		print 'The second roll value is: %d' % second_roll
  		sleep(1)
  		total_roll = (first_roll + second_roll)
  		print 'The total roll value is: %d' % total_roll
  		print 'Result...'
  		sleep(1)
  	if user_guess > total_roll:
      	print "You Won!! Great Job!"
  			return
  	else:
  			print 'You have lost. Try again.'

roll_dice(6)


#2

I see that I was using both tabs and spaces. Is it better for me to use one over the other?

Also, on line 14 I was trying to print the string of max_value, it should be max_val.


#3

Yes, use spaces or tabs.

Why this part of the code:

print "Maxium Value is " + str(max_value)
sleep(1)
user_guess = get_user_guess()

has deeper indentation than the lines before?

The first if construction has a very small indentation, why?

I mean, this is not a black magic. Delete all indentations and start from there using only spaces or tabs.


#4

I did just that and now it is working. I see where I went wrong. Thank you.


#5

Great :slight_smile: You're welcome


#6

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