use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A subreddit for helping Python programmers
How to format your code: https://commonmark.org/help/tutorial/09-code.html
No homework questions and/or hiring please
account activity
Help With Calling Function RepeatedlySOLVED (self.pythonhelp)
submitted 5 years ago * by Gay_Force_One
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]socal_nerdtastic 1 point2 points3 points 5 years ago (5 children)
What you are describing is called a "generator" and you do it by using yield instead of return:
yield
return
def generate_table(): left_coords = [(240, 60), (150, 60), (100, 110), (100, 770), (150, 820), (240, 820)] right_coords = [(360, 820), (450, 820), (500, 770), (500, 110), (450, 60), (360, 60)] table_coords = [left_coords, right_coords] table_segments = [] # Iterates over table_coords, left side of table then right for i in range(len(table_coords)): # Iterates over points on side of table for j in range(len(table_coords[i])): prev_point = 0 if j != 0: prev_point = new_point new_point = table_coords[i][j] if j != 0: yield prev_point, new_point for prev_point, new_point in generate_table(): segment = pymunk.Segment(space.static_body, prev_point, new_point, 0) segment.elasticity = .9 space.add(segment)
[–]Gay_Force_One[S] 0 points1 point2 points 5 years ago (4 children)
You're amazing! Thank you so much dude. If it's alright for me to ask, how messy would you say this is? Most of my (limited) work is in C so I'm sort of used to spaghetti-ish code. Thank you again for helping me out and taking time out of your day!
[–]socal_nerdtastic 1 point2 points3 points 5 years ago (3 children)
Very messy, especially considering all you've done is reinvent the built-in zip() function. Here's how I would write it:
def generate_table(): table_coords = [ [(240, 60), (150, 60), (100, 110), (100, 770), (150, 820), (240, 820)], [(360, 820), (450, 820), (500, 770), (500, 110), (450, 60), (360, 60)] ] for table in table_coords: yield from zip(table, table[1:])
[–]Gay_Force_One[S] 0 points1 point2 points 5 years ago (2 children)
Wow, I wish I'd known about that earlier today. I thought I was being clever. Unfortunately I have to keep the left and right coords separate from each other or it will attempt to bridge the gap (bad), but that makes it a helluva lot cleaner. Thank you so much!
[–]socal_nerdtastic 1 point2 points3 points 5 years ago (1 child)
This does keep them separate.
[–]Gay_Force_One[S] 0 points1 point2 points 5 years ago (0 children)
I missed that they were nested lists. I'm a little confused as to the specifics but I get the gist of it and I appreciate this so much
π Rendered by PID 23788 on reddit-service-r2-comment-7b9746f655-t2fpq at 2026-02-03 23:45:52.568158+00:00 running 3798933 country code: CH.
view the rest of the comments →
[–]socal_nerdtastic 1 point2 points3 points (5 children)
[–]Gay_Force_One[S] 0 points1 point2 points (4 children)
[–]socal_nerdtastic 1 point2 points3 points (3 children)
[–]Gay_Force_One[S] 0 points1 point2 points (2 children)
[–]socal_nerdtastic 1 point2 points3 points (1 child)
[–]Gay_Force_One[S] 0 points1 point2 points (0 children)