Python 2 Number Guess exercise, indentation error

With reference to this exercise: https://www.codecademy.com/courses/learn-python/projects/number-guess

I’m currently stuck on step 17 of the Number Guess exercise, which has exposed an error in my syntax. Entering “python NumberGuess.py” into the terminal produces the following error message:

“$ python NumberGuess.py
File “NumberGuess.py”, line 10
from random import randint
^
IndentationError: unexpected indent”

My code has been entered as follows:

from random import randint
from time import sleep

  def get_user_guess():
    guess = int(raw_input("Guess a number: "))
    return 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: %d" % max_val
    guess = get_user_guess()
     if guess > max_val:
      print "Invalid guess"


roll_dice(6)

Screencap here:

The error message points to line 10, ‘’‘from random import randint’’’ . I’ve tried changing the format slightly a couple of times in response but still receive the same error message. I feel like I’m missing something obvious here, any feedback on what it could be?

I am unable to get line 10 to throw the error. Does line 10 have any spaces before it?

However your indentation is a bit messy.

For example, line 13 is indented without reason.
Indentation is only used in Python to indicate lines of code that belong to a specific block.
Such as with a for or while loop, an if/elif/else statement, and when creating a class.

from random import randint     #when not part of a block, there is no
                               #indentation

def random_addition(n1):
  new_number = n1 + randint(0, 100)  #note the indentation
  return new_number     #since these lines have the same indentation
                        #they are part of the same block


random_value = random_addition(7) #since this is not indented  
                                  #it is not part of the same block

Your indentation is also inconsistent, you have some lines indented with a single space, and others indented with double spaces. Though it is not absolutely needed, it is best practice to stick with a single amount for your indentation.

2 Likes

You can also paste the code in a text editor and clean the indentation. The CC editor (at least for me) is kind of clumsy when you’re trying to correct indentation manually. It’s not representative of any real-world circumstance (paper, vim, and nano are much easier to indent properly with :slight_smile: … and these are the traditional “gotcha” platforms for people who depend too much on text editor to clean their work)

1 Like