What did I do wrong?


#1



https://www.codecademy.com/courses/python-beginner-2W5v7/1/2?curriculum_id=4f89dab3d788890003000096#


"Did you create a variable called word?" is the error message

code should work . what did i do wrong?

pyg = 'ay'

original = raw_input('Enter a word:')

if len(original) > 0 and original.isalpha()
    word=original.lower()
    first=word[0]:
    print original
else:
    print 'empty'


#2

Did you read the error code on the console? It says: "Invalid syntax", and shows where your problem is with the ^ under your code. Your error is on this line of your code. Do you see it? :slight_smile:


#3

It seems like it wanted the variable outside of the IF statement even though the directions said to put it inside the IF statement. when I stuck the variables above the if statement, it worked.


#4

I also noticed this error too:

You forgot to add a : after your if statement. Without that, your code will throw another error and not execute.


#6

So you passed that section then? Well, this is a suggestion, next time you should keep your variables nestled under the if/elif/else statements as the instructions say. :slight_smile:


#7

Could you explain why it's important to keep the variables nestled under the if statement?


#8

if/elif/else statements are sort of like a function, executing if something is true/ false.
Basic example:

number = 5 #here, we have are variable, number. 5 is stored in it

if number == 5: #here's our first statement. If number (5) is equal to 5...
   print number #number is printed

else: #if number is not equal to 5, we execute this statement...
   print "None" #..and print none

If we didn't nestle the print number under our statements, the statements won't work, and Python will throw an error.
There's not much else to say. I mean, if you decided to put print number above your statements, it will still print 5, but if you changed the value of number to 6 (number = 6), Python will execute the second statement which is the else statement. Like I said above, these are great for checking if something is true or false, and as you get more advanced, you can do much more with these statements rather than checking if a number/string is true/ false.


#14

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