CPSC120A
Fundamentals of Computer Science I

Lab 11

Conditionals

Use the command line to create a new directory called lab11 in your labs directory. Make sure all of the .py files that you create for this activity are in that directory.

Grade Conversion

You would think students would be used to the letter grade system. However, I still have students ask how their letter grade is relates to their numeric grade. Let's write a program to help these students applying what you just learned about conditionals.

Details

In a file called grades.py, write a function called convert_grade(numeric). This function takes an integer which should be greater than 0, and represents some students grades. Your function should return a string with a single character, either 'A', 'B', 'C', 'D', or 'F'. Use the following scale for your conversions.

A
[90, 100]
B
[80, 90)
C
[70, 80)
D
[60, 70)
F
[0, 60)

Note that square brackets mean inclusive, and parenthesis means exclusive.

Make sure to test your function by calling the function multiple times with different parameters. Make sure your code follows the course's code conventions.

Test Cases

Function Parameters Expected Output
95 A
90 A
89 B
80 B
79 C
70 C
69 D
60 D
59 F

Hint

Make sure you pay attention to order. If you chose your order carefully (such as starting at the F range) you can simplify your conditions greatly.

Syntatically we don't care about having multiple returns in a function. However, stylistically we want to limit the number of return statements. For the time being, only use one return statement. To do so, store what you are returning in a variable, that you create at the beginning of the function.

Challenge

Most users like more levels of specification. For example, on the Roanoke College campus we use the +/- system as well. Add code to your function so that it can also output A+'s and D-'s. Recall that there is no such thing as an F+ or F-.

Higher or Lower?

Conditional statements allow us to choose whether or not to execute a block of code. With this, in conjunction with the random module and the input function, we can start creating simple games. Today you will write your first such game.

Details

Create a function play_higher_lower(start_range, end_range) in a file called higher_lower.py. This function takes two integer parameters, the starting value of the range to generate numbers and the ending value of the range, both inclusive. The function should contain the entirety of a game of higher or lower.

The function should pick a random integer in the specified range that the user has to try to guess. It should give the user 10 chances to figure out the number. The function should prompt the user for their guess. If the guess is correct, it should tell them they got the correct number. If the user's guess is higher than the randomly generated number, it should tell the user that their guess is too high. If the user's guess is lower than the randomly generated number, it should tell the user that their guess is too low.

The function does not need to handle ending the game if the user guesses the number before the total number of guesses. Just continue prompting them for guesses.

Make sure to test your function by calling the function multiple times with different parameters. Make sure your code follows the course's code conventions.

Example

$ python3 higher_lower.py
I have picked a random number in the range 0-10.  Try to guess the
number!
guess? 5
Your guess is lower.
guess? 9
Your guess is higher.
guess? 7
Your guess is lower.
guess? 8
You got it!
guess? 0
Your guess is lower.
$

Hint

Use the random.randint function to generate your random number in the specified range.

The input function always returns a string. To compare the user's input to a number it must first be converted to a number using the built-in int function.

The function only needs 3 conditions: equality, greater than, or less than. You can accomplish this with 3 different if statements, or you can use if, elif, and else statements as well.

Challenge

Alter your code so that the number of guesses the user has is based off of the range of numbers. Also add code that informs the user if their guess is outside of the range.

Submission

Please show your source code and run your programs for the instructor or lab assistant. Only a programs that have perfect style and flawless functionality will be accepted as complete.