[Tutor] Invalid syntax (pyflakes E)

dn PyTutor at DancesWithMice.info
Tue Jul 13 14:33:21 EDT 2021


On 14/07/2021 03.17, Chris Barnes wrote:
> Below is my code.  I'm a student in Programming for Engineers and I'm
> having trouble with some syntax errors.  I've highlighted in yellow where
> I'm having the issue.  Any help would be appreciated.

Adding to @Alan's response.

Lack of visibility (for us) implies too much guessing...


> import pint;

No need for terminating semi-colon


> def GetInputFromArray(promt, choices):
>     print(prompt)

promt or prompt?


>     for i in range(0, len(choices)):
>         print("Enter {} for {}".format(i, choices[i]))

Are you new to Python after learning some other language? Beyond
language syntax, Python has its own idiom(s) (which require 'learning'
in order to release their full power/benefit) - am also throwing-in
f-strings, because they (seem to me) easier to read:

for counter, choice in choices:
    print( f"Enter { counter } for { choice }" )


>     selection = int(input("Enter Value: "))

The (non-syntax) problems with this are that you have assumed the user
will enter the numeric value, rather than the "choice"; and that if the
data-received is anything other than an integer, 'crash'!


>     return choices[selection]
> 
> a = GetInput("Select Pressure", ["atm", "in_Hg", "psi"])
> print("You selected: {}".format(a))


GetInputFromArray or GetInput?

Also, pythonista generally prefer "get_input" as a format for function
names.


> v = 1.0
> 
> from pint import UnitRegsitry
> ureg = UnitRegistry()

UnitRegsitry or UnitRegistry?

Again, pythonista prefer all import-s to be together at the top of the
code.

There are times when it makes sense to import the same module, or
components thereof, in multiple ways. However, the code 'here' does not
actually use "pint" after importing it (first import statement). If that
observation is correct, why bother?

The two lines can be condensed:
from pint import UnitRegistry as ureg

but...

> v = v * ureg.parse_expression(a)
> 
> print(" Here is the value with units {}".format(v))
> 
> print(v.to(ureg.atm))
> 
> print(v.dimensionality)
> 
> ureg = UnitRegistry(autoconvert_offset_to_baseunit = True)

What's the intention here? UnitRegistry was imported and then
re-labelled and employed as ureg. Why now (quite legally) refer to it as
UnitRegistry? Is a lack of consistency, part of the problem?

Worse: the name "ureg" is being re-used, and for something quite different.

Once it has been calculated what is its purpose?


> def getPressureInAtm():
>     print("You will input the pressure value and then select the units of
> the input")
>     p = float(input("Enter Pressure:"))
>     choices = ["atm"", "in_Hg", "psi"]
>     print("Youl will now select the units of the value entered")
>     for i in range(0, len(choices)):
>         print("Enter {} for {}".format(i, choices[i]))
>     units = int(input("Enter Value: "))
>     p1 = p * u.parse_expression(choices[units])
> 
> print(getPressureinAtm())

This seems to be largely a repeat of code 'higher up'. What is the
intention?

Proof-reading after typing will help. Read each err.msg carefully.
Usually the answer is 'right there' (although such is far too easy for
an experienced coder to say!). Be aware that sometimes the line-number
mentioned is where the error became obvious, but the actual error may
have occurred 'higher-up', eg unbalanced quotes, brackets, etc.
-- 
Regards,
=dn


More information about the Tutor mailing list