Please use python 3 programming language Assume the user has a bunch of courses
ID: 3712507 • Letter: P
Question
Please use python 3 programming language
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 course, we will ask if they have more courses to enter. If they answer 'Yes', or 'y' we continue. Anything else, we will stop and show the final GPA.
This requires us to write code that follows this logic:
1. We will use these variables: units, grade, totalpoints, totalunits and GPA. Decide their type.
2. We also need another variable, which will be used to decide to continue the loop or not. Let's call it Continue. It starts at True. Inside the loop, this variable will change to False based on asking the user.
3. The condition for the while will be based on the Continue variable. If True, we enter the loop and ask the user for a grade, and number of units.
4. A grade is legal if between 0 and 4. units is legal if between 1 and 5.
5. If either grade or units are illegal, show an error message - do not perform steps 6 or 7.
6. For better practice, write a function that returns True if the grade is legal, and False otherwise - this is optional.
7. Do the same for units - this is optional.
8. If both grade and units are legal, we accumulate units*points into a totalpoints variable.
9. We must also accumulate the totalunits variable.
10. Now we ask the user if they have more courses to enter. If they answer 'Yes', or 'y', we continue to loop. If not, we must stop the next iteration by assigning the Continue variable to False.
11. Once the loop ends (after the loop) the GPA is the totalpoints divided by the totalunits.
12. What do you need to do to show how many courses the user took? Write that code.
Explanation / Answer
def main():
Continue = True #intializing the variable as True
total_credits = 0 #initializing other variables
number_of_courses = 0
while(Continue): #while loop with condition
print("Do you have courses to enter (Yes/y)") #prompting for input
option = input() # taking input
if(option == "y" or option == "Yes"): #checking the input
number_of_courses = number_of_courses + 1 #incrementing the number of courses by one
print("Enter the grade , between 0 and 4") # prompting for input
grade = input() #taking input
if(grade < 0 or grade > 4): #checking input
print("Invalid grade, try again") #if invalid, contiuing to next iteration
continue
else:
print("Enter the number of credits, between 1 and 5") #prompting for input
credit = input() #taking input
if(credit < 1 or credit > 5): #checking input
print("Invalid credit, try again") #if invalid, continuing to next iteration
continue
else:
total_credits = total_credits + credit * grade #adding the number of credits to total
else:
Continue = False #if other than(Yes/y), continue will be false
gpa = total_credits / number_of_courses #calculating gpa
print("Your GPA is")
print(gpa) #printing gpa
main() #calling main funtion
Output :
Do you have courses to enter (Yes/y)
y
Enter the grade , between 0 and 4
3
Enter the number of credits, between 1 and 5
3
Do you have courses to enter (Yes/y)
y
Enter the grade , between 0 and 4
2
Enter the number of credits, between 1 and 5
7
Invalid credit, try again
Do you have courses to enter (Yes/y)
Yes
Enter the grade , between 0 and 4
3
Enter the number of credits, between 1 and 5
5
Do you have courses to enter (Yes/y)
no
Your GPA is
8