Question

GPA calculator: Assume the user has a bunch of courses they took, and need to calculate...

GPA calculator:

Assume the user has a bunch of courses they took, and need to calculate the overall GPA.
We will need to get the grade and number of units for each course.
It is not ideal to ask how many courses they took, as they have to manually count their courses. Programming is about automation and not manual work.

We will create a textual menu that shows 3 choices.

1. Enter course grade and units.
2. Show GPA.
3. Quit.

The user is expected to enter 1, and then enter a course grade and its number of units. They can then choose 1 and enter another course grade and units, or choose 2, and the code displays their GPA based on all the courses entered so far. If the user enters 3, the system quits. After the user chooses options 1 or 2, the code executes that menu option then shows the menu again. When the user chooses menu option 3, we do not repeat the menu.

This requires us to write code that follows this logic:

  1. Write a function named display which has no arguments, and shows the following to the screen:

    1. Enter course grade and units.
    2. Show GPA.
    3. Quit.

    then asks the user for a number to enter 1,2, or 3. If the user types 1,2, or 3 the function returns that value, otherwise the function displays an error message, and returns -1.
  2. Write code for a function named main, that does the following - (main has no arguments):
    • Call the function display (from step 1), and if the return value is 1
    • Get a grade and units from the user. Pass each to a function named legal (see below) to check if the grade is legal (between 0 and 4), and the units are legal (between 1 and 5).
    • If legal returns a true value on grade and its valid range, and legal returns a true value on units and its valid range, then accumulate grade*units to a global variable, and accumulate the units to another global variable.
      Otherwise (either grade or units is illegal) display an error message.
    • If from step 1 above, display returns 2, show the current GPA which is totalPoints/totalUnits.
    • Call main after the user enters 1 or 2 (yes main will call itself - this is recursion in its simplest form). This allows the code to display the menu again and the user gets to pick to enter more grades, or see their GPA, or quit.
    • What do you need to do if display from step 1 return -1 so the user sees the menu and gets to try again?
    • If step 1 above returns 3, then show a goodbye message. Make sure NOT to call main in this case.
  3. The function legal takes 3 integers: amount, upperBound and lowerBound. The function returns a true value if amount is more than or equal to lowerBound and less than or equal to the upperBound. Otherwise the function returns a false value.
  4. What do you need to do to show how many courses the user took? Write that code.

It is common practice to place the main function code at the bottom and place the other functions above main. If you write more functions that call other functions, then the function that gets called is placed towards the top while the function that calls other functions is placed towards the bottom.

NOTE: DO NOT USE WHILE LOOP AND MAKE SURE THAT YOU FOLLOW THE INSTRUCTIONS CAREFULLY OR YOU WILL SURELY GET A THUMBS DOWN


use Python
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution for above problem statement in Python

'''

Online Python Compiler.
Code, Compile, Run and Debug python program online.
Write your code in this editor and press "Run" button to execute it.

'''
totalPoints = 0
totalUnits = 0

def display():
print("1. Enter course grade and units.")
print("2. Show GPA.")
print("3. Quit.")
  
choice = input()
if choice in ['1','2','3']:
return int(choice)
else:
print("Invalid input")
return -1

def legal(amount,lowerBound,upperBound):
if(amount>=lowerBound and amount<=upperBound):
return True
else:
return False
  
def main():
global totalPoints
global totalUnits
choice = display()
if choice == 1:
grade = int(input("Enter grade : "))
unit = int(input("Enter unit : "))
b1=legal(grade,0,4)
b2=legal(unit,1,5)
if(b1==True and b2==True):
totalPoints=totalPoints+(grade*unit)
totalUnits = totalUnits+unit
else:
print("Either grade or units is illegal.")
  
main()
  
if choice == 2:
currentGPA = totalPoints/totalUnits
print("Current GPA = "+str(currentGPA))
main()
  
if choice ==3:
return
  
if choice == -1:
main()
main()

Screenshot of the output:

Add a comment
Know the answer?
Add Answer to:
GPA calculator: Assume the user has a bunch of courses they took, and need to calculate...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Python GPA calculator: Assume the user has a bunch of courses they took, and need to...

    Python GPA calculator: Assume the user has a bunch of courses they took, and need to calculate the overall GPA. We will need to get the grade and number of units for each course. It is not ideal to ask how many courses they took, as they have to manually count their courses. Programming is about automation and not manual work. We will create a textual menu that shows 3 choices. 1. Input class grade and number of units. 2....

  • Has to be written in Python 3 GPA calculator: Assume the user has a bunch of...

    Has to be written in Python 3 GPA calculator: Assume the user has a bunch of courses they took, and need to calculate the overall GPA. We will need to get the grade and number of units for each course. It is not ideal to ask how many courses they took, as they have to manually count their courses. Programming is about automation and not manual work. We will use a while loop and after getting the data of one...

  • Write the following program in C++. Review structures, pointers and dynamic memory allocation from CSIT 839....

    Write the following program in C++. Review structures, pointers and dynamic memory allocation from CSIT 839. Also, review pointers and dynamic memory allocation posted here under Pages. For sorting, you can refer to the textbook for Co Sci 839 or google "C++ sort functions". I've also included under files, a sample C++ source file named sort_binsearch.cpp which gives an example of both sorting and binary search. The Bubble sort is the simplest. For binary search too, you can refer to...

  • C++ programming For this assignment, write a program that will act as a geometry calculator. The...

    C++ programming For this assignment, write a program that will act as a geometry calculator. The program will be menu-driven and should continue to execute as long as the user wants to continue. Basic Program Logic The program should start by displaying a menu similar to the following: Geometry Calculator 1. Calculate the area of a circle 2. Calculate the area of a triangle 3. Quit Enter your choice(1-3): and get the user's choice as an integer. After the choice...

  • You will create a class to store information about a student�s courses and calculate their GPA....

    You will create a class to store information about a student�s courses and calculate their GPA. Your GPA is based on the class credits and your grade. Each letter grade is assigned a point value: A = 4 points B = 3 points C = 2 points D = 1 point An A in a 3 unit class is equivalent to 12 grade points (4 for the A times 3 unit class) A C in a 4 unit class is...

  • Problem Specification: Write a C++ program to calculate student’s GPA for the semester in your class. For each student, the program should accept a student’s name, ID number and the number of courses...

    Problem Specification:Write a C++ program to calculate student’s GPA for the semester in your class. For each student,the program should accept a student’s name, ID number and the number of courses he/she istaking, then for each course the following data is needed the course number a string e.g. BU 101 course credits “an integer” grade received for the course “a character”.The program should display each student’s information and their courses information. It shouldalso display the GPA for the semester. The...

  • Create a GPA calculator in Java. Ask the end user for the credit hours and letter...

    Create a GPA calculator in Java. Ask the end user for the credit hours and letter grade for each class. Once the user is finished with the data entry, calculate and output the overall GPA for the given input. For example, if four classes are input, each of which is a three-hour course, with grades of A, B, B, and B, the overall GPA will be 3.25. The exact user interaction is up to you. As long as the requirements...

  • Matlab a) Write a MATLAB code that takes the grade letters from the user and provide him/her with the GPA for that seme...

    Matlab a) Write a MATLAB code that takes the grade letters from the user and provide him/her with the GPA for that semester Enter your grades (letters): AABBC (input) Your GPA is 3.2 output) b) Write a Matlab user defined function to that takes a letter grade and return a numeric grade as shown in the table below. Any other value should give an error message (Invalid Grade). You function name should be Letter2Grade Use the following information to calculate...

  • This question was answered by someone else but their code doesn't work when I try to...

    This question was answered by someone else but their code doesn't work when I try to run it. Q1. Currently, you are enrolled in the Spring semester and taking five courses. Write a program that asks the user to enter the final score of each course. The program should display a letter grade for each class, a statement based on a letter grade, and a spring semester GPA. Each course has 3 credit hours. Write the following functions in the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT