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?

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!")
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.

How about an actual WHILE condition instead of just “true”?

Right now, your code is:
Print the current state of the game.
Check for the win condition.
Make a move for both racers.
Declare a win.
Repeat.

What if, instead, you:
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.