Question

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. Display Grade Point Average 3. Exit.
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 show which has no arguments, and shows the following to the screen: 1. Input class grade and number of units. 2. Display Grade Point Average 3. Exit. 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 grade, that does the following:
    * Call the function show (from step 1), and if the return value is 1
    * Get a grade and units from the user. Pass each to a function named valid (see below) to check if the grade is allowed(between 0 and 4), and the units are allowed(between 1 and 5).
    * If valid returns a true value on grade and its allowed range, and valid returns a true value on units and its allowed 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, show returns 2, display the current GPA which is totalPoints/totalUnits.
    * Call grade after the user enters 1 or 2 (yes grade 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 grade in this case.
3. The function valid 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.
Add the call to activate grade at the very end.
Once you finish writing the code make sure to test it properly before uploading. Add comments as you see fit but make sure to add top level comments.

REMEMBER ONLY 3 FUNCTIONS SHOULD BE WRITTEN : SHOW, GRADE, AND VALID.

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

#code screenshot

#code to copy

totalPoints=0
totalUnits=0
couses=0

def show():
    print(f"\n\n1. Input class grade and number of units. \n2. Display Grade Point Average \n3. Exit.")
    choise=int(input("Enter The Input:"))
    if choise in [1,2,3]:       #if choise is not from 1,2 and 3
        return choise
    else:
        print("Error!, Please Enter valid choise")
        return -1

def valid(amount,upporbound,lowerbound):
    if amount in range(lowerbound,upporbound+1):        #amout>= lowerband and amount<=upperband , easily do using range function
        return True
    else:
        return False

def grade():
    while True:
        choise=show()
        if choise==1:
            while True:
                print("Enter Grade from 0 to 4 and Units from 1 to 5")
                grade=int(input("Enter Class Grade::"))
                no_of_units=int(input("Enter Units::"))
                if valid(grade,4,0) and valid(no_of_units,5,1):
                    global totalPoints              #using global variable here
                    global totalUnits
                    global couses
                    couses+=1
                    totalPoints += grade*no_of_units
                    totalUnits += no_of_units
                    break
                else:
                    print("\nPlease Enter Grade and units in valid range")
        elif choise==2:
            print(f"\nTotal Courses:{couses}    Total Point:{totalPoints}     TotalUnit:{totalUnits}")
            gpa=totalPoints/totalUnits
            print(f"Your GPA is:{gpa:.2f}")

        elif choise == 3:
            break               #if choise 3 than exit

if __name__ == '__main__':
    grade()

#output screenshot

Add a comment
Know the answer?
Add Answer to:
Python GPA calculator: Assume the user has a bunch of courses they took, and need to...
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
  • 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....

  • 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...

  • 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...

  • CE – Return and Overload in C++ You are going to create a rudimentary calculator. The...

    CE – Return and Overload in C++ You are going to create a rudimentary calculator. The program should call a function to display a menu of three options: 1 – Integer Math 2 – Double Math 3 – Exit Program The program must test that the user enters in a valid menu option. If they do not, the program must display an error message and allow the user to reenter the selection. Once valid, the function must return the option...

  • 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...

  • 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...

  • Need help writing this code in python. This what I have so far. def display_menu(): print("======================================================================")...

    Need help writing this code in python. This what I have so far. def display_menu(): print("======================================================================") print(" Baseball Team Manager") print("MENU OPTIONS") print("1 - Calculate batting average") print("2 - Exit program") print("=====================================================================")    def convert_bat(): option = int(input("Menu option: ")) while option!=2: if option ==1: print("Calculate batting average...") num_at_bats = int(input("Enter official number of at bats: ")) num_hits = int(input("Enter number of hits: ")) average = num_hits/num_at_bats print("batting average: ", average) elif option !=1 and option !=2: print("Not a valid...

  • 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...

  • in Python: Write a program that allows the user to enter a number between 1-5 and...

    in Python: Write a program that allows the user to enter a number between 1-5 and returns an animal fact based on what the user entered. The program should 1. Continue to run until the user enters “quit” to terminate the program, and 2. Validate the user’s input (the only acceptable input is 1, 2, 3, 4, 5 or “quit”). You can use the following animal facts: An octopus is a highly intelligent invertebrate and a master of disguise. Elephants...

  • unctions with No Parameters Summary In this lab, you complete a partially prewritten Python program that...

    unctions with No Parameters Summary In this lab, you complete a partially prewritten Python program that includes a function with no parameters. The program asks the user if they have preregistered for art show tickets. If the user has preregistered, the program should call a function named discount() that displays the message "You are preregistered and qualify for a 5% discount." If the user has not preregistered, the program should call a function named noDiscount() that displays the message "Sorry,...

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