简体   繁体   中英

Python Turtle: how can I coordinate the maximum and minimum sized circle to thier colour?

This is for a school task I have completed all the tasks except this one "Determine the colour of the minimum and maximum sized dots and output the result eg The maximum sized dot was orange with a size of 100". I have already found the minimum and maximum sized circles I just can't figure out how to also find the colour of the maximum and minimum sized circles so that I can print ('The maximum size circle is',size,'and it is',colour)

from turtle import *

import random

randomSize=[]
randomColour=[]
countr=0 
countg=0


question=input('Do you want to run the program?')

while question=='yes':

#this function will calculate the minimum and maximum circle size form the list randomSize

  def minMax(sizes):
    minimum=maximum=sizes[0]
    for i in sizes[1:]:
      if i<minimum:
       minimum=i
 #im having trouble coordinating the circles size to its colour
      elif i> maximum:
       maximum=i

  print('The maximum size of circles was',maximum)
  print('The minimum size of circles was',minimum)

  #this function is used to find out whether the there are an even or odd number of red or green 
  dots

  def oddoreven(count,colour1):
    result=count%2
    if result==0:
      print('There were an EVEN number of',colour1, 'dots',count)
    else:
      print('There were an ODD number of',colour1, 'dots',count)

 number=int(input('How many dots do you want?'))

 for i in range (number):
   x=random.randint(-100,100)
   y=random.randint(-100,100)

   hideturtle()
   penup()
   colours=['pink','purple','black','red','green','blue']
   colour=random.choice(colours)

   if colour=='red':
     countr+=1
   elif colour=='green':
     countg+=1

   size=random.randint(0,100)
   randomSize.append(size)
   randomColour.append(colour)
   totalarea=0

   goto(x,y)
   dot(size,colour)

 print("There were",countr,"red dots and", countg,"green dots")

 oddoreven(count=countr, colour1='red')
 oddoreven(count=countg,colour1='green')
 minMax(randomSize)

 #this works out the cumulative area for all the values in the list of sizes
 for i in randomSize[1:]:
   area=3.14*(i**2)
   totalarea=totalarea+area  
 print('The cumulative area for all of the circles is',totalarea)


 question= input('Do you want to run the program again?')

Since you appended the values in the list, I can only assume that randomSize[0] is the size of randomColour[0], so on and so forth.

randomSize.append(size)
randomColour.append(colour)

Since these are parallel arrays:

randomSize.append(size)
randomColour.append(colour)

A simple fix might be to pass randomColour into minMax() as well and track the index as you loop:

def minMax(sizes, colours):
    minimum = maximum = sizes[0]
    minColour = maxColour = colours[0]

    for index, i in enumerate(sizes[1:], start=1):
        if i < minimum:
            minimum = i
            minColour = colours[index]
        elif i > maximum:
            maximum = i
            maxColour = colours[index]

    print('The maximum size of circles was', maximum, "in", maxColour)
    print('The minimum size of circles was', minimum, "in", minColour)

Of course, when we see parallel arrays, it usually implies a missing data structure of some sort. Beyond this there are bugs in your code, the most glaring of which is:

randomSize.append(size)
# ...
dot(size,colour)
# ...
for i in randomSize[1:]:
    area = 3.14 * (i**2)

The dot() method treats its size argument as a diameter but in your calculation of area, you're treating it as a radius . (The turtle circle() method takes a radius as its argument.) Below is my rework of your code to address the above and other issues:

import math
import turtle
import random

COLOURS = ['pink', 'purple', 'black', 'red', 'green', 'blue']

def minMax(diameters, colours):
    ''' calculate the minimum and maximum circle diameters and their colours '''

    minimumDiameter = maximumDiameter = diameters[0]
    minimumColour = maximumColour = colours[0]

    for index, diameter in enumerate(diameters[1:], start=1):
        if diameter < minimumDiameter:
            minimumDiameter = diameter
            minimumColour = colours[index]
        elif diameter > maximumDiameter:
            maximumDiameter = diameter
            maximumColour = colours[index]

    print("The maximum diameter of circles was", maximumDiameter, "in", maximumColour)
    print("The minimum diameter of circles was", minimumDiameter, "in", minimumColour)

def oddOrEven(count, colour):
    ''' find out whether the there are an even or odd number of red or green dots '''

    result = count % 2

    if result == 0:
        print("There were an EVEN number of", colour, "dots", count)
    else:
        print("There were an ODD number of", colour, "dots", count)

randomDiameters = []
randomColours = []

countRed = 0
countGreen = 0

answer = 'yes'

while answer.lower().startswith('y'):

    number = int(input("How many dots do you want? "))

    for _ in range(number):
        x = random.randint(-100, 100)
        y = random.randint(-100, 100)

        turtle.hideturtle()
        turtle.penup()

        colour = random.choice(COLOURS)
        diameter = random.randint(0, 100)

        # parallel arrays
        randomDiameters.append(diameter)
        randomColours.append(colour)

        turtle.goto(x, y)
        turtle.dot(diameter, colour)

        if colour == 'red':
            countRed += 1
        elif colour == 'green':
                 countGreen += 1

    print("There were", countRed, "red dots and", countGreen, "green dots.")

    oddOrEven(count=countRed, colour='red')
    oddOrEven(count=countGreen, colour='green')

    minMax(randomDiameters, randomColours)

    # work out the cumulative area for all the values in the list of diameters
    totalArea = 0

    for diameter in randomDiameters:
        area = math.pi * (diameter / 2) ** 2
        totalArea += area

    print("The cumulative area for all of the circles is", totalArea)

    answer = input("Do you want to run the program again? ")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM