As usual, create a directory to hold today's files. All programs that you write today should be stored in this directory.
$ cd ~/cs120/labs $ mkdir lab11 $ cd lab11
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.
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.
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.
Function Parameters | Expected Output |
---|---|
95 | A |
90 | A |
89 | B |
80 | B |
79 | C |
70 | C |
69 | D |
60 | D |
59 | F |
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.
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-.
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.
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.
$ 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. $
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.
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.