Activity: Practice with Ch. 1 and Ch. 2

Create a file called activity11.py in cs100/ch2.

  1. Write a function tossCoin() that simulates the flipping of a coin. It should return boolean True or False depending on whether a heads appears (True) or not (False). Test it with the following code:
    for i in range(10):
        print( tossCoin() )
    
  2. Write Python code that invokes tossCoin() 1,000 times and counts the number of heads and tails in an accumulator variable. When finished, it should print the number of times that it landed heads. Run this simulation again. What do you observe about this large number of coin tossing? Write in comments.

  3. Write a function toCelsius(temperatureF) that takes Fahrenheit degrees as a parameter and converts to Celsius degrees. (Hint: C = (F-32)*(5/9)). Test it with the following data:
    F = 32 --> C = 0.0
    F = 14 --> C = -10.0
    F = 86 --> C = 30.0
    

    This function should NOT return the value, rather just print the Celsius degree.

  4. Create a new function toFarenheit(temperatureC) that takes the temperature in celsius as input and returns the temperature in Fahrenheit. Call it a few times to test that it works and print the returned value from the function calls. How are the two function calls different? Write in comments.

  5. Write a function getMax(a,b,c) that returns the maximum of three numbers. Test it with:
    print( getMax(7, -4, 99) )
    print( getMax(23, 5, 0) )
    print( getMax(17, 74, 9) )
    print( getMax(7, 7, -9) )
    print( getMax(2, 2, 2) )
    print( getMax(7, 9, 9) )
    print( getMax(9, 7, 9) )
    
  6. Write a function isDivisibleBy(N, x) which takes a positive integer N and positive integer x returns True if the number x is divisible by N and False otherwise. Test your function by calling it.

  7. Write a function fizzBuzz() that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”. Call your function to test it. For example, the first few lines printed would appear as follows:
    1
    2
    Fizz
    4
    Buzz
    Fizz
    7
    8
    Fizz
    Buzz
    11
    Fizz
    13
    14
    FizzBuzz
    16
    ...
    

If you finish early

More practice with for loops

  1. Write a for loop to compute the sum of the first 50 numbers (1+2+3+….+49+50).
  2. Write a for loop to compute n! (\(1*2*3*...*n\))
  3. Write a for loop that draws a square with a turtle.
  4. Write a for loop that prints out the numbers -10, -9, -8, …., 9, 10.
  5. Write a function multiPrint which accepts two parameters: a string and a nonnegative integer representing the number of times that the string should be printed
  6. Use a for loop to approximate Pi using the Leibniz formula, which states that \(\frac{\pi}{4} = \frac{1}{1} - \frac{1}{3} + \frac{1}{5} - \frac{1}{7} + \frac{1}{9} - \cdots\)
  7. Use a for loop to approximate Pi using the Wallis formula, which states that \(\frac{\pi}{2} = \frac{2}{1} \times \frac{2}{3} \times \frac{4}{3} \times \frac{4}{5} \times \frac{6}{5} \times \frac{6}{7} \times \frac{8}{7} \times \cdots\)

More practice with turtles

  1. Use a turtle to plot the function y = 10x + 18
  2. Use a turtle to draw a yellow triangle with a red background
  3. Use a turtle to draw a rectangle with width 300 and height 100
  4. Write a function drawTriangle that takes three parameters - two side lengths and the angle between them - and draws the triangle. Call/invoke the function to test it. Hint: you’ll need to remember the starting location. You can call goto(x,y) on a turtle to move the turtle from one location to another.
  5. Write a function drawLine that takes three parameters - the length of the line, the starting x coordinate, and a turtle that will draw it. The function should draw a vertical line of the specified length beginning at (x,0).

More practice with functions

  1. Write a function called displayStudentInfo which accepts two parameters: a string representing a student’s name and a nonnegative integer representing the student’s age. The function should print a sentence about the student. For example, displayStudentInfo("Bob", 20) would print “Bob is a 20 year old student.”
  2. Write a function called isEven that takes one parameter (a nonnegative integer). The function will return the boolean value True if the given number is even, otherwise, it will return the boolean value False
  3. Write a function getGrade which accepts one parameter: a nonnegative integer score between 0 and 100. The function should return the string “A”, “B”, “C”, “D”, or “F” using the standard grading scale.
  4. Write a function positivity which accepts one parameter: an integer n. The function should print either “positive number”, “zero”, or “negative number” depending on the value of n.
  5. Write a function that returns the minimum of three numbers.
  6. Write a function that uses a turtle to draw a polygon that has n sides.

More practice with if statements and booleans

  1. Write a function getGrade(score) which takes an integer score between 0 and 100 as a parameter. The function will include a nested selection using if-else that sets the value of a variable gradePoint to 4 if score is greater than or equal to 90; to 3 if score is between 80 and 89; to 2 if score is between 70 and 79; to 1 if score is between 60 and 69; and to 0 otherwise. Test your function by calling it.
  2. Rewrite the previous selection using elif. Test your function by calling it.
  3. Write a function that returns True if x is larger than 10 and returns False otherwise.
  4. Write a function that returns True if and only if variable x is non-negative.
  5. Write a nested if statement that uses a variable snowFall and prints out:
    • “there was a hurricane today” if snowFall is over 10
    • “there was a lot of snow” if snowFall is greater than 7 but less than 10
    • “there was some snow I guess” if snowFall is greater than 3 but less than or equal to 7, and
    • “not enough snow to matter” if snowFall is less than 3

More practice with random module

  1. Create a function called isItRaining which returns False with 90% probability.
  2. Create a function called magicEightBallSays which randomly prints one of the following:
    • “It is certain.”
    • “Outlook good.”
    • “Ask again later.”
    • “Better not tell you now.”
    • “Very doubtful.”
  3. Create a variable called grade which randomly has the value A,B,C,D, or F.
  4. Create a variable called coinSide which randomly has the value 0 or 1.

How to submit

Submit your working python file to Moodle.