Learning Blog
Repetition - November 7
Python Code - Program A
Detailed Explanation and Terminology - Program A
In this section, we define a constant ROUNDING_PRECISION with a value of 1. Constants are used to store values that should not change during the execution of the program. By using a constant, we ensure that the rounding precision for the average mark remains consistent throughout the program.
Programming Concept: Constant: A constant is a variable whose value is set once and does not change during the execution of the program. In this case, the rounding precision for the average is fixed at 1 decimal place.
Programming Concept: Initialization: The constant ROUNDING_PRECISION is initialized with the value 1 at the beginning of the program, ensuring that it has a defined value before it is used to round the average mark later in the program.
In this section, we initialize four variables: mark, average, total_marks, and count. These variables are used to store the user's input marks, the calculated average, the sum of all entered marks, and the count of valid marks, respectively.
Programming Concept: Variable: A variable is a named storage unit used to store a data value that can change throughout the program. Here, mark, average, total_marks, and count are all variables that will hold different types of data as the program runs.
Programming Concept: Initialization: Each variable is initialized to a specific value:
mark is initialized to 0.0, which is a float. This will later store the user’s input marks.
average is initialized to 0.0 because the average value starts as zero until it's calculated.
total_marks is initialized to 0.0, which will accumulate the sum of all the entered marks.
count is initialized to 0.0, which will count how many marks are entered.
In this section, we prompt the user to input a mark using the input() function. The entered value is then converted to a float type using the float() function to handle decimal values.
Programming Concept: Input Function: The input() function is used to get data from the user. We use the float() function to convert that string to a floating-point number since the marks can have decimal places.
Programming Concept: Type Conversion: The float() function is used here to convert the string input (e.g., "85.5") into a floating-point number (e.g., 85.5).
In this section, a while loop is used to repeatedly ask the user to input marks until they enter -1. The loop will continue running as long as the user enters a mark that is not equal to -1. The entered mark is then added to total_marks, and the count is incremented by 1.
Programming Concept: While Loop (Iteration): The while loop is used to repeat the input process. The loop continues as long as the condition mark != -1 is true, meaning the user is still entering valid marks.
Programming Concept: Accumulator: The total_marks += mark statement is an example of using an accumulator. An accumulator stores the running total of the entered marks. The operator += adds the value of mark to total_marks each time a new mark is entered.
Programming Concept: Counter: The count += 1 statement is an example of a counter. A counter keeps track of how many times an event occurs—in this case, how many valid marks the user has entered. Each time the user enters a valid mark, the counter is incremented by 1.
In this section, the program checks to see if there is a valid mark input (count > 0). If there is, the average mark is calculated and rounded to 1 decimal place. If there are no marks entered, the program outputs the message “No marks were entered.”
Programming Concept: Conditionals: The if statement is used to check whether any marks were entered. This allows the program to handle both cases: when marks are entered and when no marks are entered.
Programming Concept: Average Calculation: The average is calculated by dividing the total of the marks by the number of marks entered. The result is rounded to one decimal place.
Programming Concept: Rounding: The round() function ensures the average is presented with one decimal point.
Python Code - Program B
Detailed Explanation and Terminology - Program B
In this section, we define a constant ROUNDING_PRECISION with a value of 1. Constants are used to store values that should not change during the execution of the program. By using a constant, we ensure that the rounding precision for the average mark remains consistent throughout the program.
Programming Concept: Constant: A constant is a variable whose value is set once and does not change during the execution of the program. In this case, the rounding precision for the average is fixed at 1 decimal place.
Programming Concept: Initialization of Constant: The constant ROUNDING_PRECISION is initialized with the value 1 at the beginning of the program. This ensures that it has a defined value before it is used later in the program to round the average mark.
In this section, we initialize several variables used in the program:
num_marks: This variable is used to store the number of marks the user will input.
mark: This variable will store each individual mark entered by the user during the loop.
total_marks: This variable will accumulate the sum of all marks entered by the user.
average: This variable will store the final calculated average of the marks.
Programming Concept: Variable Initialization: These variables are initialized with default values before they are used. Initializing variables ensures they have a defined value at the start of the program, preventing errors during execution.
In this section, the program prompts the user to input the number of marks they wish to enter. The input is then converted into an integer using int() and stored in the variable num_marks. This value will determine how many times the program will ask for individual marks.
Programming Concept: Input: The input() function is used to get user input. The input is converted into an integer using int() so that it can be used in the loop as the number of times the program will request marks.
Programming Concept: Data Type Conversion: The input is initially a string, but it needs to be converted to an integer because the number of marks is a whole number and will be used in a loop to control how many times the user will enter marks.
In this section, a for loop is used to iterate num_marks times, asking the user to input each individual mark. The mark is converted into a floating-point number (decimal) using float() and added to the running total of marks stored in total_marks.
Programming Concept: For Loop: A for loop is used to repeat a block of code a specific number of times. In this case, the loop runs num_marks times, once for each mark the user enters.
Programming Concept: Accumulator: The variable total_marks is an accumulator, which stores the running total of all marks entered by the user. The += operator is used to add each new mark to the running total.
In this section, the program calculates the average of all entered marks if at least one mark was provided (num_marks > 0). The average is calculated by dividing total_marks by num_marks, and the result is rounded to 1 decimal place using the round() function. If no marks were entered, a message is displayed to inform the user.
Programming Concept: Conditional Statement: The if statement checks whether the user entered any marks. If num_marks > 0, the average is calculated; otherwise, a message is displayed.
Programming Concept: Average Calculation: The average is calculated by dividing the sum of the marks (total_marks) by the number of marks (num_marks). The result is rounded to 1 decimal place to meet the specified precision.
Programming Concept: Rounding: The round() function is used to round the average to a specified number of decimal places, which in this case is controlled by the constant ROUNDING_PRECISION.
Conclusion
These terms help us understand the structure and function of a program. Mastering these terms is crucial to learning programming as they form the basic concepts and mindset of programming. In the learning blog I share, these terms can be combined to enhance the understanding and interpretation of the code.