How to make a tiebreaker on Turtle Race?
Question:
Hey guys I am doing the turtle race module on 100 days of code and I have been able to get it to work so far. However I am having trouble coding a tiebreaker function into it.
I used boolean based "if" statements to make a rudimentary tiebreaker but it only works if your guess in the game is one of the two winners. If there is a tie and neither one of the winners are your guess I can only get it to say "You lost."
Does anyone have any idea on how I can code this a little better?
from turtle import Turtle, Screen
import random
is_race_on= False
screen = Screen()
screen.setup(500, 400)
user_bet = screen.textinput(title="Make your
bet",prompt= "Which turtle will win the race?
Enter a color: ")
colors = ['red', 'orange', 'yellow', 'green',
'blue', 'purple']
y_positions= [-125, -75, -25, 25, 75, 125]
all_turtles= []
for turtle_index in range(0, 6):
new_turtle = Turtle()
new_turtle.color(colors[turtle_index])
new_turtle.penup()
new_turtle.shape("turtle")
new_turtle.goto(x= -230, y=
y_positions[turtle_index])
new_turtle.pendown()
all_turtles.append(new_turtle)
win = False
lose = False
if user_bet:
is_race_on = True
while is_race_on:
for turtle in all_turtles:
if turtle.xcor() > 230:
is_race_on= False
winning_color = turtle.pencolor()
if winning_color == user_bet:
win = True
win_color = winning_color
else:
lose = True
lose_color = winning_color
if win and lose == True:
print(f"There was a tie between
{win_color}, and {lose_color}")
elif win == True and lose == False:
print(f"You've Won!! The {win_color}
turtle is the winner!")
elif win == False and lose == True:
print(f"You lost you loser you! The {lose_color} turtle is the winner!!")
random_distance= random.randint(0, 10)
turtle.forward(random_distance)
screen.exitonclick()
Answers:
I would order this a little different. Beginning of the while loop I would check the position of each turtle and any turtle that’s crossed the finish line I would save to a winners list. If the length of the list is 0, then no one has won. You can then loop through the turtles again, incrementing them, and start again. If a turtle HAS won and the size of the list is 1, you can pay out that winner. If the list is > 1 you have a tie and can deal with that as you see fit. This causes you to loop through the turtles twice but organizes yourself a bit better. Hopefully this makes sense.
Hey guys I am doing the turtle race module on 100 days of code and I have been able to get it to work so far. However I am having trouble coding a tiebreaker function into it.
I used boolean based "if" statements to make a rudimentary tiebreaker but it only works if your guess in the game is one of the two winners. If there is a tie and neither one of the winners are your guess I can only get it to say "You lost."
Does anyone have any idea on how I can code this a little better?
from turtle import Turtle, Screen
import random
is_race_on= False
screen = Screen()
screen.setup(500, 400)
user_bet = screen.textinput(title="Make your
bet",prompt= "Which turtle will win the race?
Enter a color: ")
colors = ['red', 'orange', 'yellow', 'green',
'blue', 'purple']
y_positions= [-125, -75, -25, 25, 75, 125]
all_turtles= []
for turtle_index in range(0, 6):
new_turtle = Turtle()
new_turtle.color(colors[turtle_index])
new_turtle.penup()
new_turtle.shape("turtle")
new_turtle.goto(x= -230, y=
y_positions[turtle_index])
new_turtle.pendown()
all_turtles.append(new_turtle)
win = False
lose = False
if user_bet:
is_race_on = True
while is_race_on:
for turtle in all_turtles:
if turtle.xcor() > 230:
is_race_on= False
winning_color = turtle.pencolor()
if winning_color == user_bet:
win = True
win_color = winning_color
else:
lose = True
lose_color = winning_color
if win and lose == True:
print(f"There was a tie between
{win_color}, and {lose_color}")
elif win == True and lose == False:
print(f"You've Won!! The {win_color}
turtle is the winner!")
elif win == False and lose == True:
print(f"You lost you loser you! The {lose_color} turtle is the winner!!")
random_distance= random.randint(0, 10)
turtle.forward(random_distance)
screen.exitonclick()
I would order this a little different. Beginning of the while loop I would check the position of each turtle and any turtle that’s crossed the finish line I would save to a winners list. If the length of the list is 0, then no one has won. You can then loop through the turtles again, incrementing them, and start again. If a turtle HAS won and the size of the list is 1, you can pay out that winner. If the list is > 1 you have a tie and can deal with that as you see fit. This causes you to loop through the turtles twice but organizes yourself a bit better. Hopefully this makes sense.