Python Forum
Python 25 Line Challenge
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python 25 Line Challenge
#17
If you remove comments and spacing, got it to 31 lines.
from random import choice, randint
from turtle import Turtle, write, goto, penup, pendown, speed, hideturtle
from time import sleep

# List of 10 colors
colors = ['yellow', 'orange', 'red', 'pink', 'purple', 'blue', 'green', 'brown', 'gray', 'black']

# Hide the turtle and draw the start and finish line
hideturtle(), speed('fastest')
penup(), goto(-200, 10*30/2), pendown(), goto(-200, 0-270)
penup(), goto(200, 10*30/2), pendown(), goto(200, 0-270)

class MyTurtle(Turtle):
    '''Create turtle objects and set properties'''
    def __init__(self, pos):
        super().__init__()
        color = colors.pop(randint(0, len(colors)-1)) if colors else None
        self.color(color), self.shapesize(2)
        self.penup()
        self.goto(-200, (pos*30) - (10*30/2))

# Create a list of turtle objects
turtles = list(MyTurtle(i-2.5) for i in range(1, 11))

# Set a variable for accessing turtle objects
i = 0

# Setup a while loop
racing = True
while racing:
    # Create a random distance to travel and icrease i
    turtles[i := (i + 1) % len(turtles)].forward(randint(0, 5))

    # Track movement of turtle object in list
    # First to line wins goes to home and writes winner
    # Sleep for five seconds to see the winner
    if turtles[i].xcor() >= 200:
        winner = turtles[i]
        winner.write(f'winner: turtle {i+1}', font=('system', 25, 'bold'))
        sleep(5)
        racing = False

Updated code. Now 25 lines without spaces and comments
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts


Reply


Messages In This Thread
Python 25 Line Challenge - by codingCat - May-02-2022, 01:08 PM
RE: Python 25 Line Challenge - by codingCat - May-02-2022, 01:11 PM
RE: Python 25 Line Challenge - by menator01 - May-02-2022, 08:02 PM
RE: Python 25 Line Challenge - by codingCat - May-03-2022, 11:23 AM
RE: Python 25 Line Challenge - by Gribouillis - May-03-2022, 12:24 PM
RE: Python 25 Line Challenge - by codingCat - May-04-2022, 06:52 PM
RE: Python 25 Line Challenge - by menator01 - May-03-2022, 07:49 PM
RE: Python 25 Line Challenge - by codingCat - May-04-2022, 06:55 PM
RE: Python 25 Line Challenge - by Gribouillis - May-03-2022, 09:24 PM
RE: Python 25 Line Challenge - by codingCat - May-04-2022, 07:06 PM
RE: Python 25 Line Challenge - by menator01 - May-05-2022, 07:10 PM
RE: Python 25 Line Challenge - by Coricoco_fr - May-09-2022, 01:11 PM
RE: Python 25 Line Challenge - by codingCat - May-09-2022, 02:27 PM
RE: Python 25 Line Challenge - by menator01 - May-06-2022, 07:33 PM
RE: Python 25 Line Challenge - by codingCat - May-09-2022, 11:49 AM
RE: Python 25 Line Challenge - by codingCat - May-09-2022, 12:17 PM
RE: Python 25 Line Challenge - by menator01 - May-09-2022, 06:04 PM
RE: Python 25 Line Challenge - by codingCat - May-09-2022, 06:33 PM
RE: Python 25 Line Challenge - by menator01 - May-09-2022, 06:36 PM
RE: Python 25 Line Challenge - by Coricoco_fr - May-11-2022, 05:34 PM
RE: Python 25 Line Challenge - by Gribouillis - May-09-2022, 06:30 PM
RE: Python 25 Line Challenge - by menator01 - May-09-2022, 06:33 PM
RE: Python 25 Line Challenge - by Gribouillis - May-09-2022, 06:51 PM
RE: Python 25 Line Challenge - by menator01 - May-09-2022, 07:03 PM
RE: Python 25 Line Challenge - by Gribouillis - May-09-2022, 07:07 PM
RE: Python 25 Line Challenge - by codingCat - May-10-2022, 01:43 PM
RE: Python 25 Line Challenge - by codingCat - May-10-2022, 02:55 PM
RE: Python 25 Line Challenge - by Gribouillis - May-10-2022, 07:03 PM
RE: Python 25 Line Challenge - by Gribouillis - May-11-2022, 05:51 AM
RE: Python 25 Line Challenge - by codingCat - May-11-2022, 11:33 AM
RE: Python 25 Line Challenge - by codingCat - May-12-2022, 07:10 PM
RE: Python 25 Line Challenge - by codingCat - May-13-2022, 07:00 PM
RE: Python 25 Line Challenge - by codingCat - May-17-2022, 01:50 PM
RE: Python 25 Line Challenge - by codingCat - May-17-2022, 06:24 PM
RE: Python 25 Line Challenge - by codingCat - May-18-2022, 07:17 PM
RE: Python 25 Line Challenge - by kkcreator - Dec-20-2024, 03:00 PM
RE: Python 25 Line Challenge - by woodturner - Dec-24-2024, 12:39 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python 100 line Challenge codingCat 9 7,499 Jun-20-2022, 07:18 AM
Last Post: Coricoco_fr
  Zen Python Challenge ichabod801 3 5,149 Aug-13-2018, 12:02 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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