Python "Bad syntax"

Christian Gollwitzer auriocus at gmx.de
Mon Feb 5 12:30:01 EST 2018


Am 05.02.18 um 18:13 schrieb darkorbitaknaentou at centrum.cz:
> 
> Hi, I have a problem in continuing the function.
> 
> I'm a beginner, I'm learning from a textbook. I'm going to put the 
> following examples from a textbook that displays "wrong syntax"
> 
>>>> for letter in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
>              if letter in "AEIOU":
>                  print(letter, "is a vowel")
>             else:
>                   print(letter, "is a consonant")
> 
> In this text, I will write a "wrong syntax" after confirming the "else" 
> function. How is it possible? Using the Bad Version of Python? Please, 
> please, thank you very much!

Make sure you don't mix tabs and spaces and make consistent indentation. 
In your above example, the problem is that "if" is indented one more 
space than "else". If I paste this into ipython (3.6.1), I get:

File "<ipython-input-3-21b11970a3df>", line 4
     else:
          ^
IndentationError: unindent does not match any outer indentation level

which is a pretty clear error. I'm not sure which version of Python you 
are using which only displays "wrong syntax" instead of a descriptive 
error message. I suggest in any case to use IPython for interactive use.

> 
> The same error is in the "break" function:
> 
>>>> while True:
>                item = get_next_item()
>                if not item:
>                     break
>                process_item(item)

I can't see an error here, it only fails for me because the functions 
"get_next_item" and "process_item" are not defined. Maybe the 
tabs/spaces were mixed in your original input?

	Christian



More information about the Python-list mailing list