Question: This Python assignment use conditional statements, boolean functions, and while loops. ----------------------------------------------------------------------------------- import random import turtle def isInScreen(win,turt): -----leftBound = -win.window_width() / 2 -----rightBound =

This Python assignment use conditional statements, boolean functions, and while loops.

-----------------------------------------------------------------------------------

import random

import turtle

def isInScreen(win,turt):

-----leftBound = -win.window_width() / 2

-----rightBound = win.window_width() / 2

-----topBound = win.window_height() / 2

-----bottomBound = -win.window_height() / 2

-----turtleX = turt.xcor()

-----turtleY = turt.ycor()

-----stillIn = True

-----if turtleX > rightBound or turtleX < leftBound:

----------stillIn = False

-----if turtleY > topBound or turtleY < bottomBound:

----------stillIn = False

-----return stillIn

def main():

-----wn = turtle.Screen()

-----# Define your turtles here

-----june = turtle.Turtle()

-----june.shape('turtle')

-----while isInScreen(wn,june):

----------coin = random.randrange(0, 2)

----------if coin == 0:

--------------june.left(90)

----------else:

--------------june.right(90)

----------june.forward(50)

-----wn.exitonclick()

main()

-----------------------------------------------------------------------------------

Modify the program in the following ways:

  1. Write appropriate comments for this program and function. Include:
    1. A header comment for the program
    2. A header for the function describing the parameters and return value.
    3. Comment the blocks of code. The text has descriptions of all this, so reread that if necessary.
  2. Create a new turtle, a black hole turtle, that moves to some random location on the screen and draws a black dot of diameter 100. Use the .dot function instead of .circle, since you want the turtle at the center of the figure. [As of Fall 1, 2018, there is a bug in the active code window. If you are using the textbook to write this program, use may have to use dot(50) to get the correct size black hole. If you are using a stand-alone python compiler, use dot(100).] This should be done in the main function before creating the randomly walking turtle.
  3. Write a boolean function that has two turtles as parameters, and returns true if the turtles are within 50 units of each other and false otherwise. There are several ways to determine the distance between two turtles. You could use math.sqrt or math.hypot to compute the distance. If you read the turtle documentation, you may find another way as well. Talk to the instructor if you are having trouble with this. Be sure to comment this function.
  4. Modify the while loop so that the program stops when the turtle leaves the screen or when it touches the black hole. Use your Boolean function from Step 3.
  5. Modify the function isInScreen so that the function only returns false if the turtle wanders off the top or bottom of the screen. The next step is going to deal with the turtle moving off the screen horizontally.
  6. Modify the body of the while loop so that if the turtle wanders off the screen either to the right or the left, it reappears on the opposite side of the screen. See the movie BlackHoleTrim.mp4 Play media comment.
  7. Add another feature to your program. Either more randomness, or one more randomly walking turtle, or multiple black holes, or ??? This doesn't have to be too fancy, but should be more than just adding one line of code.
  8. Write a reflection in the comment box answering the following questions:
    1. Describe your added feature from Step 7. Show the code that accomplishes this.
    2. Right now, the function determining if turtles are close to each other uses a constant value of 50. How can you change the function so that it is more general

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!