< Back

Lab 20: Sounds


Practice Problem 1

Write a function called convert_to_GPA(alphabet_grade), which takes a string which contains just one character. This character must be one of "ABCDF". Your function should print the GPA associated for that grade:

Letter Grade Numeric Grade
A4.0
B3.0
C2.0
D1.0
F0.0

Example

>>> convert_to_GPA("A")
4.0
>>> convert_to_GPA("D")
1.0
>>> convert_to_GPA("F")
0.0

Hint

You are directly given an String as a parameter. You simply need to convert this string into a floating point value. There are many ways to do this, but the easiest to understand is likely using if statements.

For each entry in the table above, you need one if statement. if the letter grade equals A, then output 4.0, etc.

Don't forget, to compare equality you want to use the == operator.


Practice Problem 2

Write a function called print_factors(x), which prints all of the positive integer factors of \(x\). An integer \(y\) is an integer factor of \(x\) if the remainder of \(\frac{x}{y}\) is 0.

Example

>>> print_factors(1)
1
>>> print_factors(2)
1
2
>>> print_factors(4)
1
2
4

Hint

The remainder operator in Python is \(\%\). \( x \% y == 0\) tells you that y evenly divides x.

Write a loop that goes through all of the integers greater than 1, but less than (or equal to) \(x\). See if the above condition is true. If it is true, print the loop variable.


Decrease Volume

In a file called decrease_volume.py write a function called decrease_volume(a_sound). This function should modify the passed in sound to be quieter than previous.

Example

Before

Nato Phonetic Alphabet Normal

After

Nato Phonetic Alphabet Quiet

Hint

Sounds are represented as a collection of samples. Each one of these sample represents an "amplitude" of the wave form of the sound. In order to decrease the volume of a sound, you want to decrease its amplitude.

You need to iterate over the samples of your sound for this assignment. Just like we were doing with getPixels(a_picture), you can get a list of the samples using the getSamples(a_sound) function.

To get the value of a specific sample of the sound, you can use the getSampleValue(sample) function. This is very similar to getRed(pixel) for images.

You want to decrease the amplitude. This means you should multiply the amplitude by a fraction, like \(\frac{1}{4}\).

To set the sample value for your sound, you use the setSampleValue(sample, value) function. This is like the setRedValue(pixel, color) function for images.