HW 4

Create a file called hw04.py

Store it in your CS100/hw folder.

You are to write 4 functions on the topics of functions, loops, and conditionals.

You should put all 4 functions in the file you created and submit that to Moodle.

  1. Write a function called numberDescription() that takes in one parameter and prints out a message that correctly states the kind of number it is. Here are the possible messages:

Positive Integer (i.e 182), Positive Decimal (i.e. 8.031), Zero, Negative Integer (i.e. -94), Negative Decimal (i.e. -456.12)

After you write the function, call it with each of those numbers above to demonstrate that the program works correctly.

  1. Write a function called randomPolygon() that takes one parameter to specify the number of sides that the polygon has. It then draws that many sides and returns to the same starting location. Every vertex in the polygon will be randomly chosen. Note: The starting location could be anywhere on the drawing canvas; the polygon should start from wherever the turtle was before the randomPolygon() function was called. See the examples below:

Random Polygon Example1 Random Polygon Example2 Random Polygon Example3 Random Polygon Example4 Random Polygon Example5

#    Called with 4        Called with 5       Called with 5         Called with 7         Called with 30

In your program, call this function with the parameter 5 three times, and the parameter 10 once.

  1. Write a function checkThree() that takes three parameters and prints one of the following messages:

“All three values are the same”, “Two of the values are equal”, “All three values are different and the middle value is:” (provide the middle value)

checkThree(7,7,7) 
All three values are the same

checkThree(8,3,8)
Two of the values are equal

checkThree(0,0,5)
Two of the values are equal

checkThree(2,9,12)
All three values are different and the middle value is: 9

checkThree(4,1,9)
All three values are different and the middle value is: 4

checkThree(2,20,17)
All three values are different and the middle value is: 17

Call this function with the six example inputs here.

  1. Write a function called twoBoxDots() that takes in 1 parameter that specifies how many random dots to draw to the canvas.

Before drawing the dots, the program should draw two boxes on the canvas. (See the picture below). The function should then draw the random dots. If the dot is in either box, it should be RED, if it is not in the either box, it should be BLUE. Finally, the program should print the number of dots in the first box, the number of dots in the second box, and how many dots were not in either box.

Two Boxes with Dots

Call this function with 100 dots.

Grading

In order to receive any credit, your program must run without syntax errors in Thonny. Thonny reports syntax errors in red in the shell area; if Thonny outputs an error, your code isn’t usable. Code that doesn’t run is just text - it’s not a program. It’s an important part of programming and problem solving to come up with a solution that can be shared with others.

Your program doesn’t have to be perfect to be eligible for credit! It’s okay to have some semantic errors. Semantic errors don’t stop your program from running, but they cause your program to produce an unexpected or incorrect result. For example, if your program is supposed to calculate the sum of the first 100 numbers, but instead it accidentally calculates the sum of the first 101 numbers, that would be a semantic error. Semantic errors may result in partial credit depending on how close your solution is.

How to submit

Make sure that all of the code that you wrote for this assignment has been included in your .py file. Make sure that you have named in hw04.py Double check that your program runs without syntax errors. Submit the file `hw04.py` updated with your changes to Moodle.