Indentation error:


#1


https://www.codecademy.com/en/courses/python-ext/projects/number-guess

Indentation error, unexpected indent

Run like a normal code but it doesnt


"""This program rolls a pair of dice and adds both values then asks user to guess the number and compares it to original value. If the user's roll is greater than total value of dice roll, user wins and vice versa. Also inform user"""


from random import randint

def get_user_guess():
  	user_guess = int(raw_input('Guess number'))
  	return user_guess

def roll_dice(number_of_sides):
  number_of_sides= int(raw_input('please enter number of sides a dice should have'))
  first_roll= randint(1,number_of_sides)
  second_roll= randint(1, number_of_sides)
  roll_dice= first_roll + second_roll
  max_val= number_of_sides *2
  print 'The max possible value is' + str(max_val)
  user_guess= get_user_guess()
  if user_guess> max_val:
      print 'Guess invalid'  
      return
  else:
      print 'The first roll is %d' % first_roll
			print 'The second roll is %d' % second_roll
      print 'The total is %d ' % roll_dice
          if user_guess>roll dice:
        	    print 'you won'
          else:
              print 'you lost' 
              return
            
roll_dice(number_of_times)


#2

Why are the code blocks inside get_user_guess indented twice? They should only be indented once.


#3

here:

      print 'The total is %d ' % roll_dice
          if user_guess>roll dice:

why does this lines not have the same indent level?

@harrypotter91, you are right, its a bit uncommon but not what causes the error


#4

So then what types of indentations cause errors?


#5

this does:

      print 'The total is %d ' % roll_dice
          if user_guess>roll dice:

there is no reason for additional indent after a print statement


#6

I mean why does the function's indent not cause an error?


#7

removed all unnecessary indents. still causing errors:

https://www.codecademy.com/en/courses/python-ext/projects/number-guess


#8

because indention is required/expected, but python gives you the freedom to determine how much you want the indent to be (4 spaces is common, but not mandetory)


#9

The exercise url is just the same for everyone, please copy paste your code to the forum

Did you also see my suggestion?


#11

"""This program rolls a pair of dice and adds both values then asks user to guess the number and compares it to original value. If the user's roll is greater than total value of dice roll, user wins and vice versa. Also inform user"""

from random import randint

def get_user_guess():
  	user_guess = int(raw_input('Guess number'))
  	return user_guess

def roll_dice(number_of_sides):
  number_of_sides= int(raw_input('please enter number of sides a dice should have'))
  first_roll= randint(1,number_of_sides)
  second_roll= randint(1, number_of_sides)
  roll_dice= first_roll + second_roll
  max_val= number_of_sides *2
  print'The max possible value is' + str(max_val)
  user_guess= get_user_guess()
  if user_guess> max_val:
      print 'Guess invalid'  
      return
  else:
      print'The first roll is %d' % first_roll
			print'The second roll is %d' % second_roll
      print'The total is %d ' % roll_dice
      if user_guess>roll dice:
          print'you won'
      else:
          print'you lost' 
          return
            
roll_dice(number_of_times)

#12

Why it doesnt post the code like it did in the first instance??


#13

because you need to use format your code:

here:

      print'The first roll is %d' % first_roll
			print'The second roll is %d' % second_roll
      print'The total is %d ' % roll_dice

why does the print statement of second_roll have so much indent? You do understand how indent works and why its important in python?


#15

"""This program rolls a pair of dice and adds both values then asks user to guess the number and compares it to original value. If the user's roll is greater than total value of dice roll, user wins and vice versa. Also inform user"""


from random import randint

def get_user_guess():
  	user_guess = int(raw_input('Guess number'))
  	return user_guess

def roll_dice(number_of_sides):
  number_of_sides= int(raw_input('please enter number of sides a dice should have'))
  first_roll= randint(1,number_of_sides)
  second_roll= randint(1, number_of_sides)
  roll_dice= first_roll + second_roll
  max_val= number_of_sides *2
  print'The max possible value is' + str(max_val)
  user_guess= get_user_guess()
  if user_guess> max_val:
      print 'Guess invalid'  
      return
  else:
      print'The first roll is %d' % first_roll
			print'The second roll is %d' % second_roll
      print'The total is %d ' % roll_dice
      if user_guess>roll dice:
          print'you won'
      else:
          print'you lost' 
          return
            
roll_dice(number_of_times)

#16

I got your indent point. The print line shows greater indent here. I dont know why. In the editor, it doesnt!
something's wrong


#17

Here you go


#18

i see, weird. Usually i would re-indent/rewrite this lines if such errors occur then


#19

Maybe I should it try it on actual compiler?


#20

given python is a interpreted language and not a compiled one, that would be really impressive. Maybe you can find an online tool to re-indent the entire code? I use VIM, but unless you know this editor, its a pain to do in this editor


#21

Its sure is a pain. I have been trying this for hours now. Gets on nerves at times. Will try doing that on VIM


#22

i would find another tool, VIM is a command line text-editor, that will take you hours as well to figure that out