Python Forum
Need to fix SyntaxError in cycle try
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need to fix SyntaxError in cycle try
#2
There is so much wrong that this really can't be called Python code.

In response to one of your earlier posts I mentioned that you do not define functions inside loops. Instead of this:
for i in range(10):
    def square():
        return i**2
Do this:
def square(number):  # Declare function outside of loop
    return number**2

for i in range(10):
    print(square(i))   # Call function inside of loop.
I don't know what you are trying to do with this:
try:
    ht == 0
    if True:
        lh0 = i, o[0], o[1], l
        return lh0
        print(lh0)
except:
    pass
Are you trying to do this?
if ht == 0:
    return i, o[0], o[1], l
try/except catch exceptions. An exception is raised when your program tries to do something but fails. An example is trying to open a file. Your program has a reasonable expectation that opening a file should work, but maybe the file was deleted. Exceptions are not used to fix bad code, they are used to handle potential errors that can occur when executing correct code.

Syntax errors are not exceptions, they are invalid code. You need to correct the code. Try/except cannot catch a syntax error because syntax errors prevent running your program.

You should not use == when comparing float numbers. Your equation may return a value really close to zero, but it is unlikely any combination of arguments will produce a result that is exactly zero. That is just the nature of floating point math on computers; ridiculous resolution, but almost never results that are exactly right. Read about it here:

https://docs.python.org/3/tutorial/floatingpoint.html

Instead of testing == 0, use math.is_close() or numpy.is_close().

https://docs.python.org/3/library/math.html
https://numpy.org/doc/stable/reference/g...close.html
if math.isclose(ht, 0, abs_tol=1.0e-9)
Try to clean up some of these errors and if you are still having problems post your updated code and an explanation of what you are trying to do. Last time I tried to help I was hindered by never really knowing what you wanted to do, and your code was not helping my understanding.
alexfrol86 likes this post
Reply


Messages In This Thread
RE: Need to fix SyntaxError in cycle try - by deanhystad - Mar-24-2022, 06:09 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Writing a cycle to find the nearest point from the array Tysrusko 0 269 May-10-2024, 11:49 AM
Last Post: Tysrusko
  Trying to cycle through a list of charcters pooky2483 12 4,819 Sep-28-2020, 06:55 AM
Last Post: pooky2483
  <while> cycle is not interrupted when using the <random>module ShityCoder 3 2,331 Sep-04-2020, 04:05 PM
Last Post: ShityCoder
  Cycle of numpy variables Zero01 0 1,634 Jul-31-2020, 11:58 AM
Last Post: Zero01
  stop cycle while windows11 1 2,127 May-16-2020, 03:17 PM
Last Post: deanhystad
  Occurrences using FOR and IF cycle P86 2 2,644 Jul-29-2019, 04:37 PM
Last Post: ThomasL
  pool map cycle skorost5 5 4,021 Apr-07-2019, 09:21 AM
Last Post: skorost5
  lifetime cycle counter Ixxxxxx 0 2,582 Mar-07-2018, 07:26 PM
Last Post: Ixxxxxx

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020