# Help with Python Code Requirements

Hello,
This code is for the Tortoise and the Hare scenario. I am helping a python student and they understand the concepts but I am new to python myself.

Trying to improve this code to meet the requirements below but have a challenge with a few requirements.
Here is what needs to be accomplished.

1. Cannot use BREAKS. What can we use instead of BREAKS?
2. I printed the Steps for the Tortoise and the Hare, I wanted it flush right. Only was able to accomplish with many spaces in the print statement. Any better way to do this?
3. Loop is running 69 times, need it to run 70
4. After it prints the winner, â€śHare Wins!â€ť it prints out one more line after. Cant figure out why?

``````import random

tortoiseStep = 0
hareStep = 0

# returns the number of steps to move
# positive steps means move to right
# negative steps means movie to left
def Tortoise_Move() -> int:
#a random number in range 1 - 10
randomNum = random.randint(1,10)
# if number is in range 1 - 5 it is a fast plod
# so a 50 % probabibility
if (randomNum in range(1,6)):
return 3
#if number is 6 or 7 the tortoise slips
elif(randomNum in range(6,8)):
return -6
#if number is 8, 9 or 10 the tortoise slow prods
else :
return 1

# similarly for hare
def Hare_Move() -> int:
#random number between 1 - 10
randomNum = random.randint(1,10)
# if number is between 1-3, hare sleeps
if (randomNum in range(1,3)):
return 0
#if number is 3 or 4 the hare does a big hop
elif(randomNum in range(3,5)):
return 9
#if number is 5 the hare big slips
elif(randomNum == 5):
return -12
#if number is 6, 7 or 8 the hare small hops
elif(randomNum in range(6,9)):
return 1
#if number is 9 or 10 the tortoise small slips
else :
return -2

print("BANG !!!!!\nAND THEY'RE OFF !!!!!")
while(True):
for Step in range(70):
#printing the track displaying the positions of the ha/Are and tortoise
if (tortoiseStep == hareStep and Step == tortoiseStep):
print('OOPS!!',end='')
else :
if (Step == tortoiseStep):
print('T',end='')
elif(Step == hareStep):
print('H',end='')
else :
print('.',end='')
print('                                  TourtoiseStep:',tortoiseStep, 'HareStep',hareStep)
print()
#if any of the two cross the finish line the loop breaks
if(tortoiseStep >= 70 or hareStep >= 70):
break
#getting the moves for both after each loop
tortoiseNextStep = Tortoise_Move()
hareNextStep = Hare_Move()
tortoiseStep = tortoiseStep + tortoiseNextStep
hareStep = hareStep + hareNextStep
#the position should not be negative
if(tortoiseStep<0):
tortoiseStep=0
if(hareStep<0):
hareStep=0
#printing the result
if tortoiseStep >= 70:
print("Tortoise wins!!")
elif hareStep >= 70:
print("Hare wins!!")
elif hareStep >= 70 and tortoiseStep >= 70:
print("Its a Tie!")
``````
1 Like

Caution to those seeking to post in this thread: It is a homework assignment, and copying anotherâ€™s work is strictly prohibited in the assignment document. Stick to advice, not code.

Print the current state of the game.
Check for the win condition.
Make a move for both racers.
Declare a win.
Repeat.

Check for the win condition.
Make a move for both racers.
Print the state of the game.
Repeat
After the loop, declare a win.

Flush right toâ€¦ what?

No, the loopâ€™s running 70 times? Step will be 0-69. At the start of the race, by the way, that means youâ€™ll print â€śOOPS!!â€ť, because the tortoise, the hare, and the Step are all 0, as they all start at the same point. Maybe â€śBâ€ť for both would be a better solution?

Well, what happens when you do

compared to what happens when you do

?

What does that imply for your last print statement? (Also, consider which statement is your last one, because of how your code is workingâ€¦)

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.