Activity: Practice with Ch. 1 and Ch. 2

Create a file called activity11.py in cs100/ch2. Note that it its good practice to keep your function definitions together above and keep your function calls below. For example, your code will always have the following general structure.

import stuff
import otherstuff

# function definitions go here
def myFunction1():
    <code>

def myFunction2():
    <code>

def myFunction3():
    <code>

# function calls go here
myFunction1()
myFunction2()
myFunction2()
  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. 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
    F=15 --> C=-9.44
    F=85 --> C=29.44
    

    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 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
    ...
    

Optional - If you finish early

  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 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.

How to submit

Submit your working python file to Moodle.