Chris G Chris G - 4 months ago 8
Python Question

I don't understand how to fix this print indentation error

name = str(input("Whats your name?: "))
if name is 'Chris':
print("Hello, " + name)
^
IndentationError: expected an indented block


So this error shows up right beneath the "t" in
print
. I have tried putting a space after it, like so:

print ("Hello, " + name)


But that didn't work either. Anyone know what I should be doing differently?

I am using this in the Python command window as opposed to Sublime which I usually use, because it doesn't seem to read the input that is typed in.

Answer

Do it like this:

name = str(input("Whats your name?: "))
if name == 'Chris':
    print("Hello, " + name)

You had an extra " mark after name. Also, you want == not is; is is for determining if two objects are identical, meaning, the literal same object.

Unless you're doing something more advanced, you'll typically only use is to compare with None:

if variable is None:
    print("No value provided for variable!")

Also, make sure that you use the same number of spaces to indent each block of code. If you use 4 spaces, and 3 spaces somewhere else, you'll get an IndentationError.

Comments