0

I am new in python and I have this code:

s = 'azcbobobegghakl'
for (i, item) in enumerate(s):    
    print item

for (i, item) in enumerate(s):
    if item=="a" or item=="e" or item=="i" or item=="o" or item=="u" 
    vol++
    print vol

The first for works goods and show each character as should be. For the second for I have this error :

if item=="a" or item=="e" or item=="i" or item=="o" or item=="u"
                                                                     ^
SyntaxError: invalid syntax

I search this post but it not solved Pythonic String Testing

2
  • 2
    You forgot the : after the if block. Commented Jan 16, 2015 at 3:26
  • 3
    You could really simplify your code with in: if item in 'aeiouAEIOU': Commented Jan 16, 2015 at 3:26

5 Answers 5

3

Why do you enumerate when you never use the value i anyway?

s = 'azcbobobegghakl'
for letter in s:    
    print letter

The other loop was missing a colon and indentation.

vol = 0
for letter in s:
    if letter in 'aeiou': 
       vol += 1
print vol 

You can also do it like this:

print sum(letter in 'aeiou' for letter in s)
Sign up to request clarification or add additional context in comments.

4 Comments

The last one might be a bit confusing, since the keyword in are used in two different ways.
I am a PHP developer. I am starting to study python. It is why it is a basic question. By the way, your code is good. It prints 5 but show a error. %run "C:/Users/Rayn/PYTHON DOCS/6.00.1X/pset0Test.py". any idea?
%run "C:/Users/Rayn/PYTHON DOCS/6.00.1X/pset0Test.py"
That's not a python error message. It's a filepath.
2

You need the colon at the end

if item=="a" or item=="e" or item=="i" or item=="o" or item=="u":

Here's a more compact solution:

if item in ['aeiou']:

1 Comment

Your condition in the if statement is wrong. It should be if item in 'aeiou'. Otherwise the condition is only true when item is the string aeiou.
1

missing colon:

for (i, item) in enumerate(s):
    if item=="a" or item=="e" or item=="i" or item=="o" or item=="u":
        vol+=1
        print vol

This should work.

Edited: I forgot to mention that Python does not have a ++ operator.

2 Comments

I still have an error vol++ ^SyntaxError: invalid syntax
++ syntax is not used in python. Use +=1 instead.
1

You forgot the : after the if statement. You also have to indent the lines with vol++ and print vol. Furthermore incrementing in python is done by +=1, not by ++.

for (i, item) in enumerate(s):
    if item=="a" or item=="e" or item=="i" or item=="o" or item=="u":
        vol+=1
        print vol

1 Comment

That works. But it outputs %run "C:/Users/Rayn/PYTHON DOCS/6.00.1X/pset0Test.py". Any idea what is this?
0

You forgot the : on the end of the if. And the next row has to be indented.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.