Exercise 8: Word Up -- Troubleshooting Code


#1

Hello -- am stuck again on this exercise, I keep receiving a Syntax Error when trying to create a variable called word... Any suggestions/explanation greatly appreciated! Thank you in advance!

EXPECTATION:
1. Make all letters lowercase
2. Grab first letter of the word

INSTRUCTIONS
Inside your if statement:

Create a new variable called word that holds the .lower()-case conversion of original.
Create a new variable called first that holds word[0], the first letter of word.

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

MY CODE:

pyg = 'ay'
original = raw_input('Enter a word:')

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

ERROR MESSAGE:
File "python", line 4
if len(original) > 0
^
SyntaxError: invalid syntax


#2

I think I've figured out the issue, I've readjusted the code to be the following:

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'

BUT is there an explanation why the above is correct versus my original (shown below)?

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

I'd like to better understand how the if statement works and what are statment constraints/definitions (?) versus variables within the if statement.

Thank you!


#3

"BUT is there an explanation why the above is correct versus my original (shown below)? "

If you want to have multi line if's in python you have to use parenthesis (to my knowledge, there is no other way).

e.g.: The following should work.

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

If you mean to have assignment of a variable mid condition, that's a syntax error within python -- so I don't believe that's valid.


#4

Thank you @randonuser!

To make sure I understand your explanation -- to recap: for multiple lines in an if statement, parenthesis are needed to recognise the entire statement. Otherwise, the if statement is defined first, with variables assigned underneath.

Can you please explain why you'd need to use "==" in your multi-line if statementexample instead of just a single "="?


#5

to compare equality in python you use "==" even within if statements.

If you wanted to actually assign to them

e.g., in c# the following is perfectly valid

while ((line = streamReader.ReadLine()) != null)

which is assigning to a variable mid condition, python doesn't do that.

Overall:

#multi-line if, use parenthesis -- yes.
if (condition1 and
    condition2):

#for setting variables
if (condition1 and
   condition2):
   variable = value

#for checking variable equality
if (condition1 and
   condition2 and
   variable == value):

#6

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