Calculting GPA using While Loop (Python)

2024/11/18 9:43:37

A GPA, or Grade point Average, is calculated by summing the grade points earned in a student’s courses and then dividing by the total units. The grade points for an individual course are calculated by multiplying the units for that course by the appropriate factor depending upon the grade received:

  A receives 4 grade pointsB receives 3 grade pointsC receives 2 grade pointsD receives 1 grade pointF receives 0 grade point

Your program will have a while loop to calculate multiple GPAs and a while loop to collect individual grades (i.e. a nested while loop).

For your demo, calculate the GPA to 2 decimal places for these two course combinations:

 First Case:                5 units of A           4 units of B           3 units of CSecond Case:           6 units of A           6 units of B           4 units of C

This is what I have so far....

todo = int(input("How many GPA's would you like to calculate? "))
while True: x in range (1, todo+1)
n = int(input("How many courses will you input? "))
totpoints = 0
totunits = 0while True:  range(1, n+1)grade = input("Enter grade for course: " )
if grade == 'A':grade = int(4)
if grade == 'B':grade = int(3)
if grade == 'C':grade = int(2)
if grade == 'D':grade = int(1)
if grade == 'F':grade = int(0)units = int(input("How many units was the course? "))
totunits += units
points = grade*units
totpoints += points
GPA = totpoints / totunitsprint("GPA is ", ("%.2f" % GPA))
print("total points = ", totpoints)
print("total units = ", totunits)    

My question is how do I exactly incorporate the while function correctly? My code is not running correctly.

Thanks in advance.

Answer

First thing you must know about Python is that is all about indentation. So make sure you indent your while blocks correctly. The syntax of the while statements is like:

while <condition>:do_something

The code inside the while block is executed if the condition is true. After executing the code the condition is checked again. If the condition is still true it will execute the block again, and so on and so forth until the condition is false. Once the condition is false it exits the while block and keeps executing the code after.

One last thing, Python does not have case statements like other programming languages do. But you can use a dictionary, so for example I've defined a dictionary to map the grades to points and remove the if statements from your code:

gradeFactor = {'A':4, 'B': 3, 'C':2, 'D':1, 'F':0}
todo = int(raw_input("How many GPA's would you like to calculate? "))
while todo: n = int(raw_input("How many courses will you input? "))totpoints = 0totunits = 0while n:  grade = raw_input("Enter grade for course: " )units = int(raw_input("How many units was the course? "))totunits += unitspoints = gradeFactor[grade]*unitstotpoints += pointsn -= 1GPA = float(totpoints) / totunitsprint("GPA is ", ("%.2f" % GPA))print("total points = ", totpoints)print("total units = ", totunits)todo -= 1   
https://en.xdnf.cn/q/118690.html

Related Q&A

Return function that modifies the value of the input function

How can I make a function that is given a function as input and returns a function with the value tripled. Here is some pseudo code for what Im looking for. Concrete examples in Python or Scala would b…

how to access the list in different function

I have made a class in which there are 3 functions. def maxvalue def min value def getActionIn the def maxvalue function, I have made a list of actions. I want that list to be accessed in def getaction…

Replacing numpy array with max value [duplicate]

This question already has answers here:numpy max vs amax vs maximum(4 answers)Closed 2 years ago.I have an array, a = np.array([[0,9,8],[5,6,4]])how to replace the each array in axis 1 with the max val…

Finding the max and min in dictionary as tuples python

so given this dictionary im trying to find the max value and min value {Female :[18,36,35,49,19],Male :[23,22,6,36,46]}the output should be in tuples for example key: (min,max)Female: (18,49) Male: (6,…

Flask sqlalchemy relations across multiple files

Im new to Flask Sqlalchemy and I want to declare multiple models and relate them to each other, I followed the example in the documentation but I keep getting this error sqlalchemy.exc.InvalidRequestEr…

Parsing XML with Pykml

I have the following xml file I got from QGIS<?xml version="1.0" encoding="UTF-8"?><kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.…

Python Google App Engine Receiving a string in stead of JSON object

I am sending a HTTP POST request from android to a server using the script belowURI website = new URI("http://venkygcm.appspot.com");HttpClient client = new DefaultHttpClient();HttpPost reque…

Creating Gui for python client server

Help needed with my python project. Unable to get the code for the client or server implemented with the Gui i created. it is one based on book seller where the client is the buyer and the server is th…

Maximum Subarray sum - Where is my solution wrong? Kadanes Algorithm

Here is a description of the problem:The maximum sum subarray problem consists in finding the maximum sum of a contiguous subsequence in an array or list of integers: max_sequence([-2, 1, -3, 4, -1, 2,…

Pandas: Selecting rows and columns based on a subset of columns that contain a certain value

Lets say I have a dataframe with column names as follows:col_id_1, col_id_2, ..., col_id_m, property_1, property_2 ..., property_nAs an example, how would I search across all col_ids for, say, the valu…