CPTR 124 Fundamentals of Programming
In this lab you will write two programs that use loops.
- Teams
You are encouraged to work with a partner for this lab. You and your partner should begin thinking about the problems and begin writing the code before lab time.
- Part 1: Guessing Game
In Python, you can generate a pseudorandom integer by calling the
randrange
function. Place the following statement at the top of your program:from random import randrangeAfterward, each time you need a pseudorandom number, the statementx = randrange(1, 101)will assign a pseudorandom integer in the range 1...100 inclusive to the integer variablex
. Notice that the callrandrange(
m,
n)
produces a pseudorandom number in the range m...n–1; that is, the second parameter is one more than the largest pseudorandom number it can generate.The following complete program prints 10 such pseudorandom integers:
from random import randrange for i in range(10): print(randrange(1, 101))Each time the program is run it prints a different sequence of numbers.
See the textbook for more information about random numbers.
Write a Python program that plays a guessing game with the user. The program's user is the player. The player is supposed to guess an integer in the range 1–100. The program should generate a number in this range at random. The program's number is the correct answer. The program compares the player's guess to the correct number:
- If the player's number is less than the correct number, then the program should print "Too low" and allow the player to guess again.
- If the player's number is greater than the correct number, then the program should print "Too high" and allow the player to guess again.
- If the player's number is the same as the correct number, the game is over.
Within a loop, the game continues until the player guesses the correct answer. When the game ends, the program prints the total number of guesses, counting the correct guess as one of the guesses. The player's goal is to guess the number with the fewest attempts.
The following shows a sample run of the program:
Guess a value from 1 to 100: 50 Too low, please try again Guess a value from 1 to 100: 75 Too high, please try again Guess a value from 1 to 100: 64 Too high, please try again Guess a value from 1 to 100: 55 Too high, please try again Guess a value from 1 to 100: 52 Too high, please try again Guess a value from 1 to 100: 51 51 is the correct answer It took you 6 tries to get the correct answerHint: While you are developing your program you can print the answer (secret number to guess) each time a new guess is requested. That way you can better tell if your program is working properly; that is, giving you the correct feedback. Be sure to disable this "feature" in your final version, or it will not be much of a game!
- Part 2: Grade Calculator
Write a Python program that reads in letter grades for a class. Acceptable grades are A, B, C, D, and F. The user may enter as many grades as desired and enters a Z to indicate the end of the grades. The terminating letter Z does not count as a grade. No plus or minus should be attached to a grade. The program should ignore any other characters entered by the user. Your program should treat a lower-case letter like its upper-case equivalent; for example, process a b as if it were a B. If
letter
is assigned to a string, the statementletter = letter.upper()will capitalize all the characters in the string
letter
.The program should keep track of the number of students that passed (D or above) and the number that failed (F). After the user finishes entering the letter grades, the program should calculate and print the percentage of students passing and failing as well as the class grade-point average (GPA) (A = 4.0, B = 3.0, C = 2.0, D = 1.0, F = 0.0).
Any letters that the user enters that are not A, B, C, D, or F do not participate in the calculation of the pass/fail percentage and do not contribute to the GPA calculation. If the user enters a Z before entering any valid letter grades, the program should terminate without printing anything.
Sample run:
Enter grades (Z terminates the list): A B B C F D Z Students passing: 5 (83.33%) Students failing: 1 (16.67%) Class GPA: 2.17Round the GPA and percentages to two places behind the decimal point.
- Check out
Your finished programs will be evaluated for correctness and compliance. When approved, you should submit your two Python source files to http://eclass.e.southern.edu.