I'm working though a course on Python. The following code is meant to count the vowels in a string. I typed it out as I thought it should be and did not work. I then cut n paste from lecture notes and it did work.
But I cannot see what the difference is! I've commented out my non working expressions. The working expression is below it.
Why does mine not work?
## count the number of vowels in string s
s = 'azcbobobegghakl'
vowelcount = 0
for char in s:
## if char == 'a' or char = 'e' or char == 'i' or char == 'o' \
## or char == 'u':
if char == 'a' or char == 'e' or char == 'i' \
or char == 'o' or char == 'u':
vowelcount += 1
print "Number of vowels: " + str(vowelcount)
Here is the error message:
%run "/var/folders/cs/31zvz_q925v_z3tmvj09dpyc0000gn/T/tmpCEB9ME.py"
File "/var/folders/cs/31zvz_q925v_z3tmvj09dpyc0000gn/T/tmpCEB9ME.py", line 4
if char == 'a' or char = 'e' or char == 'i' or char == 'o' \
^
SyntaxError: invalid syntax
=that means you are doing assignment operation rather than comparision.