Question 2: Writing code (16 points) Now it’s time to write some code from scrat
ID: 3890675 • Letter: Q
Question
Question 2: Writing code (16 points)
Now it’s time to write some code from scratch – please use python. We recommend you use the IDLE programming environment. For this exercise, please DO NOT use the python Shell window – instead, write all your code in the Editor window, which you can open by going to File->newFile.
Note: This problem description is long because we’ve given you lots of hints on how to solve the problem - so be sure to read it all.
Problem description: The code will help the user figure out how much work they need to invest in studying for the final overall score based on the grade they want in the class. Assume that the total course grade (out of 100) is based on test1 (out of 30), test2 (out of 30) and an final overall score (out of 40). Thus,
course total = test1 + test2 + final overall score
Assume the user already wrote test1 and test2 and got some score on each (out of 30). They now want to know what score they need to get on the final overall score to get a certain course total.
What should my code do?
Your code should ask the user to input the following pieces of information (see sample run on the next page):
what total score they received on test1, out of 30
what total score they received on test2, out of 30
what total score they want in the class out of 100
The code should then print one of these two messages
- tell the user that the total score they want is not possible (e.g., this can happen if they want to get 90/100 in the class, but they did not write either of the two tests)
- if the total score they want is possible, the score out of 40 they need on the final test to get that course total.
Foundations: understanding the problem
Before we dive into the code, we need to understand the problem. We have this equation
course total = test1 + test2 + final overall score (formula 1)
And we have 3 known quantities (i.e., what the user will tell us), including: test1 score, test2 score, and the course total score. Thus, the only unknown is the final overall score. We can shuffle around the equation terms to get final overall score by itself on one side of the equation, which gives us the needed formula for calculating the final overall score the user wants to know:
final overall score = course total – test1 – test2 (formula 2)
Formula 2 is what we will need. We can then check that this formula works by plugging in some test cases:
Course total the userwants (out of 100)
Test1 score (they got on test1, out of 30)
Test2 score (score they got on test 2, out of 30)
final overall score (the score they need, out of 40)
So basically, it’s easy: the formula course total – test1 – test2 tells us the final overall score they need. This final overall score is exactly what our program needs to output, with one catch: sometimes the user might be unrealistic and want a course score they can’t get because they did poorly on the two tests. Here is an example:
Course total the user wants
Test1 score (they got on test1, out of 30)
Test2 score (score they got on test 2, out of 30)
final overall score (the score they need, out of 40)
The final overall score comes out as 80 – but that’s not possible because the final overall score is only out of 40!
Here is a situation where the user instead aims too low – they got 30/30 on both tests, and they want a course total of 50:
Course total the user wants
Test1 score (they got on test1, out of 30)
Test2 score (score they got on test 2, out of 30)
final overall score (the score they need, out of 40)
The final overall score comes out as negative!
Luckily we can easily check for these situations, i.e., the final overall score our code calculates must be greater or equal to zero and less than or equal to 40 – if not, we tell the user the score they want is not possible.
Now that we have the problem down, we still should not write code – instead, the next step is to write out the algorithm. There are many different correct ways to solve this problem, here is one potential algorithm:
Algorithm (a high level recipe you will need to translate to python):
ask the user for test1 grade, test2 grade, and the desired grade
calculate the final overall score needed (recall this is just the formula: final overall score = course total – test1 – test2)
if the final overall score needed is possible (i.e., it is greater or equal to zero and less than or equal to 40) tell the user what score they need to get on the final overall score
otherwise, the final overall score needed is not possible, and so tell the user that instead
In your code, to help you debug, use informative variable names, and make the prompts to the user meaningful (e.g., you can use the ones in the sample runs above as a template).
What should my code output look like? Here are several sample runs of the code showing you what the program in action looks like:
Run1
Enter your score for test1 (out of 30): 30
Enter your score for test2 (outof 30): 30
Enter what total you want in the class out of 100: 80
You will need to get 20 out of 40 on the final
Run 2:
Enter your score for test1 (out of 30): 0
Enter your score for test2 (outof 30): 0
Enter what total you want in the class out of 100: 80
Your desire geade is not possible
Run3:
Enter your score for test1 (out of 30): 25
Enter your score for test2 (outof 30): 20
Enter what total you want in the class out of 100: 75
You will need to get 30 out of 40 on the final
Test your program! Some possible values are in the tables above, but feel free to try more.
Note: When testing, be sure to enter correct values to the program’s questions (because the program doesn’t have to check if the user enters correct values):
enter a number between 0 and 30 for test1 when answering question 1
enter a number between 0 and 30 for test2 when answering question 2
enter a number between 0 and 100 for the desired total (when answering question 3)
Course total the userwants (out of 100)
Test1 score (they got on test1, out of 30)
Test2 score (score they got on test 2, out of 30)
final overall score (the score they need, out of 40)
80 30 30 20 70 20 20 30 55 15 15 25Explanation / Answer
test1 = int(input("Enter your score for test1(out of 30):"))
test2 = int(input("Enter your score for test2(out of 30):"))
total = int(input("Enter what total you want in class out of 100:"))
diff = total -(test1+test2)
if diff > 40:
print("Your desire is not possible")
else:
print("You will need to get ",diff,"out of 40 on the final")
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.