Syntax and name errors are entirely resolvable at your end, normally.
But Iâll have a look.
You seem to have both spaces and TAB characters for indenting; that is
usually a bad mix. TABs are interpreted as 8 spaces of indent, but often
are not rendered that way, depending on your editor. You should use all
spaces or all TABs, and try not to mix them. Youâre using spaces almost
everywhere, so Iâd stick with spaces.
Youâve got TABs here:
while X < 2:
and also on the line directly below it.
Fixing then (and guessing about your intent with the line above), the
first syntax error is line 44, âX = 0â. That is itself fine, so the
actual error is before that:
y = Q[float('inf')] is set(x in a == all(i) in J(len(x[i]) >= len(x[i+1]))
Thereâs no closing â)â for the opening one in âset(â. The âJ(âŚ)â part
goes right to the end of the line. If (guessing again about your intent)
I put a closing bracket at the end, thus:
y = Q[float('inf')] is set(x in a == all(i) in J(len(x[i]) >= len(x[i+1])))
you then have legal Python syntax. But other problems remain. Poinitng
pyflakes at your programme:
% python3 -m pyflakes p1.py
p1.py:41:48 undefined name 'J'
p1.py:49:17 local variable 'X' defined in enclosing scope on line 44 referenced before assignment
p1.py:49:13 local variable 'X' is assigned to but never used
p1.py:63:16 undefined name 'o'
Letâs a look:
p1.py:41:48 undefined name 'J'
This is again the line above. Your programme defines no function named âJâ.
Then these:
p1.py:49:17 local variable 'X' defined in enclosing scope on line 44 referenced before assignment
p1.py:49:13 local variable 'X' is assigned to but never used
p1.py:63:16 undefined name 'o'
are all the same issue from this code:
X = 0
def p():
def T():
while X < 2:
any(y) in T(p(y).eq(0)) or all(y) in T(p(y).eq(1))
X = X + 1
The first line defines âXâ in the global scope. Since you do not use âXâ
below this code, maybe âXâ was meant to be used only for the function
âTâ, which is the only place it is used. If so, you want this instead:
def p():
def T():
X = 0
while X < 2:
any(y) in T(p(y).eq(0)) or all(y) in T(p(y).eq(1))
X = X + 1
which defines âXâ in the local scope of âTâ.
What was happening before? You had defined an âXâ in the global scope.
It looked like you then wanted to use âXâ inside the T() function as a
counter. You might do that if you wanted to use âXâ later in the main
global programme. (This isnât a great way to do that - better to return
a value from the function.)
But Python has a feature about scopes that is not obvious to new
programmers: since you do not (normally) declare variables, how does
Python decide what scope holds the variable.
In functions, if the function assigns to a variable anywhere, then
variable is held to be a local variable. This lets you write:
def f():
x = 0
for i in range(4):
x = x + 2
return x
and x is a local variable, affecting nothing outside the function. But
if you only use âXâ in the functionm Python looks for it in an outer
scope:
def f():
for i in range(4):
print(y)
Here âyâ is not a local variable because it is never assigned to.
Your T() function assigns to âXâ, so thatâs local to T(). The âXâ in the
global scope is unaffected and unrelated.
Letâs go back to the modified function from your programme:
def p():
def T():
X = 0
while X < 2:
any(y) in T(p(y).eq(0)) or all(y) in T(p(y).eq(1))
X = X + 1
This still does not make a great deal of sense. Firstly, T() computed X
but never returns it. Maybe you did intend it to affect the global X?
You can do that like this:
X = 0
def p():
def T():
global X
while X < 2:
any(y) in T(p(y).eq(0)) or all(y) in T(p(y).eq(1))
X = X + 1
which tells T() that âXâ is from the global scope, not the local scope
of the function.
There are other problems here:
The function T() is called only by itself. Functions can call
thenselves, but there should be something which make the initial call to
T(), otherwise it never runs at all. Nothing else calls T().
The function p() defines T() but never calls it. A function definition
is a local assignment, just like assigning to a variable, so T() is
known only inside p(). Nothing outside can use it.
Inside T(), this line:
any(y) in T(p(y).eq(0)) or all(y) in T(p(y).eq(1))
just does some logical tests, not assigning the result to anything. It
does call T() and P() internally, so if they had side effects this might
result in something. But they donât.
This expression:
p(y).eq(0)
might be intended to test of p(y)==0, but that it actually means is
âcall p(), then call the âeqâ method of what p() returnedâ. I doubt that
is what you intend.
Also, because p() has no explicit âreturnâ statement, it returns None.
None has no .eq() method.
Just looking at your code, I think youâre trying to do symbolic set
expressions. But Python functions are not thqt - they are procedural
actions. FOr example, writing:
any(y) in ....
Means:
- call the any() function with âyâ; the Python âany()â function tests
where any of the values in it argument are true; it returns true if so
and false otherwise.
- takes the result of any(y) and tests if it in âinâ what follows. SO it
is taking a true or false value, and seeing if that value is in what
follows.
I do not think that is what you intended. I suspect you meant was âfind
values from y such that âŚâ. WHich you can do, but you donât write it
that way. You might write:
for y0 in y:
if y0 in ...:
do something with y0 here ...
The largest âprogrammingâ problem with your code is that you have a
bunch of variable with single letter names and no comments at all
describing what their purpose is.
Others have pointed out various logic errors, such as expressions like:
len(x) >= float('inf')
which can never be true (len(x) is an int, a finite integer).
Also, the larger test:
if len(x) >= float('inf') or len(x) <= float('inf'):
is always true because the second part is always true. This is
probably not what you intend.
Cheers,
Cameron Simpson cs@cskk.id.au