Chapter 6: Problem 28
Write a method quality Points that inputs a student’s average and returns 4 if the student's average is 90–100, 3 if the average is 80–89, 2 if the average is 70–79, 1 if the average is 60–69 and 0 if the average is lower than 60. Incorporate the method into an application that reads a value from the user and displays the result.
Short Answer
Step by step solution
Define the Method
Implement Logic for Quality Points
Create the Main Function
Call qualityPoints Method
Display the Result
Testing the Application
Unlock Step-by-Step Solutions & Ace Your Exams!
-
Full Textbook Solutions
Get detailed explanations and key concepts
-
Unlimited Al creation
Al flashcards, explanations, exams and more...
-
Ads-free access
To over 500 millions flashcards
-
Money-back guarantee
We refund you if you fail your exam.
Over 30 million students worldwide already upgrade their learning with Vaia!
Key Concepts
These are the key concepts you need to understand to accurately answer the question.
Method Definition
This method takes a single parameter, `average`, which represents the student's average score. A parameter is like a special kind of variable that is used to pass information into methods. By defining parameters, you let the method know what to expect as input.
- Start by declaring the method with a name like `qualityPoints`.
- Specify the parameter in the parentheses. For example, `qualityPoints(average)`.
- Describe the task the method should accomplish within its body. Here, it will return a number representing quality points based on the input average.
Conditional Statements
The common format for a conditional statement in Python is the `if-elif-else` construct. This allows your program to check multiple criteria and execute code accordingly.
- Start with an `if` statement, checking the highest condition: `if average >= 90`.
- Use `elif` for additional conditions: `elif average >= 80` to `elif average >= 60`.
- Finally, use `else` to define the action if none of the previous conditions are met.
User Input Handling
In our exercise, user input handling is done in the main function, where we ask for the student's average score. In Python, the `input()` function captures user input in the form of a string.
- Use the `input()` function to prompt and read data: `average_string = input('Enter student average: ')`.
- Convert the input string to a number, since calculations are not possible with strings: `average = int(average_string)`.
Return Values
In the `qualityPoints` method, the return value is an integer representing the quality points based on the student’s average. This is achieved using the `return` statement. The `return` statement exits the method and sends back the specified value to where the method was called.
- Within the method, use `return` followed by the value to be returned, like `return 4` when the average is between 90 and 100.
- The result from the `qualityPoints` method can then be captured in a variable when the method is called, allowing it to be used in the program's logic or output: `points = qualityPoints(average)`.