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.
- Cannot use BREAKS. What can we use instead of BREAKS?
- 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?
- Loop is running 69 times, need it to run 70
- After it prints the winner, “Hare Wins!” it prints out one more line after. Cant figure out why?
Thank you for your help!
Requirements:
https://docs.google.com/document/d/1QqZGsYTq5BvK24D3VYhByEZg0-bnmA8dz7wYqJuQinE/edit?usp=sharing
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!")