2021年4月22日星期四

Python - How can I create 10 circles at random to be selected at one time?

My goal is to create 10 circles with a random diameter which will appear randomly on my canvas. I thought the best way to do this is to use a global list of records. Thus, I created a dictionary for the circles' location as well as another dictionary for the circles' diameter. I want the user to be able to click on the circle, have it highlight to a color when selected, and then store that ID in a global variable.

Now, the concern I have is whether I am being too verbose, or if there is an easier method of solving this problem. My goal is to use a dictionary or list to complete this task, but should I be iterating over my dictionary 10 times with a for loop? Or do I need to do each key separately.

I am still a novice to python, so if my code is way off please bear with me.

# global variables  circ_loc = {'ID 1': random(0, 550), 'ID 2': random(0, 550),             'ID 3': random(0, 550), 'ID 4': random(0, 550),             'ID 5': random(0, 550), 'ID 6': random(0, 550),             'ID 7': random(0, 550), 'ID 8': random(0, 550),             'ID 9': random(0, 550), 'ID 10': random(0, 550),             'ID 11': random(0, 550), 'ID 12': random(0, 550),             'ID 13': random(0, 550), 'ID 14': random(0, 550),             'ID 15': random(0, 550), 'ID 16': random(0, 550),             'ID 17': random(0, 550), 'ID 18': random(0, 550),             'ID 19': random(0, 550), 'ID 20': random(0, 550),}    circ_diameter = {'ID 1': random(0, 550), 'ID 2': random(0, 550),             'ID 3': random(0, 550), 'ID 4': random(0, 550),             'ID 5': random(0, 550), 'ID 6': random(0, 550),             'ID 7': random(0, 550), 'ID 8': random(0, 550),             'ID 9': random(0, 550), 'ID 10': random(0, 550),             'ID 11': random(0, 550), 'ID 12': random(0, 550),             'ID 13': random(0, 550), 'ID 14': random(0, 550),             'ID 15': random(0, 550), 'ID 16': random(0, 550),             'ID 17': random(0, 550), 'ID 18': random(0, 550),             'ID 19': random(0, 550), 'ID 20': random(0, 550),}    def setup():      size(600, 600)    def draw():      background(0)      for i in circ_loc:          ellipse(i.keys().values(), i.keys().values(), random(20, 50), random(20, 50)  

Any feedback would be greatly appreciated as it helps my learning.

https://stackoverflow.com/questions/67223046/python-how-can-i-create-10-circles-at-random-to-be-selected-at-one-time April 23, 2021 at 10:04AM

没有评论:

发表评论