0% found this document useful (0 votes)
38 views3 pages

Exam Marks Calculation with Loops

Homework 5 focuses on using while loops in Python to calculate the maximum, minimum, and average of exam marks input by the user, with incomplete code sections to be filled in. Additionally, it includes a challenge to simulate rolling a die 6000 times to count the occurrences of sixes, along with a request for testing methods. The document is structured to guide students in completing programming tasks and understanding while loops.

Uploaded by

louis.mutzig
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views3 pages

Exam Marks Calculation with Loops

Homework 5 focuses on using while loops in Python to calculate the maximum, minimum, and average of exam marks input by the user, with incomplete code sections to be filled in. Additionally, it includes a challenge to simulate rolling a die 6000 times to count the occurrences of sixes, along with a request for testing methods. The document is structured to guide students in completing programming tasks and understanding while loops.

Uploaded by

louis.mutzig
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Homework 5 While loops

Introduction to Python

Homework 5: While loops


1. Here is the code for a program to find and print the maximum, minimum and average of
several exam marks (all integers) entered by the user, to the nearest whole number.
The end of input is signalled by an entry of -1.
Some lines of code are missing or incomplete. The lines are marked with *****.
Fill in the missing code.
#Program name: L5 HW5 exam [Link]
#Program find maximum, minimum and average exam mark

total = number of marks x average marks


mark = int(input("Enter first exam mark: "))
maxMark = mark+average

minMark = maxMark-average

numberOfMarks = all of the marks added together

while mark < min mark___*****


total = total + mark
if mark < minMark:
minMark = mark

if *****
maxMark = mark
mark = int(input("Enter next exam mark, -1 to end: "))

averageMark = mark *****

print ("Maximum mark =",maxMark)


print ("Minimum mark =",minMark)
print("Average mark =",averageMark)
input("Press Enter to exit ")
[7]

1
Homework 5 While loops
Introduction to Python

Challenge

2. (a) Write a program to count the number of sixes thrown in a simulation of a die being
thrown 6000 times.
Remember to import the Python module random at the start of the program.
To generate a random number between 1 and 6, use the statement
throw = [Link](1,6) [7]

(b) Describe how you could test whether your program is working correctly. [4]

2
Homework 5 While loops
Introduction to Python
[Total 18 marks]

Common questions

Powered by AI

To measure improvements in programming logic based on multiple homework assignments, various aspects should be compared: the complexity and correctness of implemented algorithms, the efficiency and clarity of code, error-handling capability, understanding and implementation of control structures like loops and conditionals, and the ability to work with different data types. Moreover, one should assess not only correctness but also the code's adaptability and maintainability, problem-solving approaches, and design patterns. Evaluating these facets over different assignments can reflect a learner's progress and understanding depth in computational logic.

The simulation of a die throw can be effectively tested for correctness in counting sixes by running the program multiple times and analyzing the statistical distribution of results. As the simulation is supposed to mimic unbiased die throws, the number of sixes should approximately follow a binomial distribution with a probability of 1/6 for each throw. Conducting at least 6000 iterations should yield around 1000 occurrences of six if the simulation is correct. Additionally, implementing assertions or consistency checks that verify each throw result is within the 1-6 range will further validate correctness.

Incorrectly setting initial values for maxMark and minMark in the program can result in inaccurate calculations of these statistics. If initialized improperly, such as setting them both to zero or an out-of-range number, the program might incorrectly handle the first input mark, either incorrectly updating or not updating these values based on the logic programmed. This error would misrepresent the maximum and minimum values either by prematurely fixing them or failing to update with subsequent input unless overwritten by a larger or smaller valid input, respectively.

If decimal marks were introduced instead of integer marks, the program logic would need several changes. Primarily, variable initializations and calculations for the sum, maximum, minimum, and average would need to adapt to float data types to accommodate decimal precision. The input method might also require validation to ensure that non-integer values correctly parse into floats. All calculations within the loop, especially those involving comparisons and arithmetic operations, must account for potential floating-point precision issues, such as ensuring averages round to the desired decimal places for consistency and correctness in output.

While loops are essential in processing sequential data input like exam marks because they allow for the repeated execution of a block of code as long as a specified condition is true. This makes while loops particularly useful for handling input data of variable length, such as user-entered exam marks, where the end condition (e.g., entering -1) is not known beforehand and dictates the termination of data entry and subsequent processing. The loop efficiently processes each piece of input data iteratively, updating relevant calculations or condition checks until the termination condition is met.

Initializing variables like total and numberOfMarks at the start of a program loop is crucial for calculating statistics because these variables store cumulative values that are updated with each iteration of the loop. Without initialization, these variables' values would be undefined, leading to incorrect calculations such as summing total marks or counting the number of inputs, as the program might operate on garbage values or produce runtime errors. This can cascade into inaccurate statistics output, including an incorrect average or possibly causing the program to crash or behave unpredictably.

If the missing lines for updating the maximum and minimum marks are not correctly implemented, several logical errors can occur. First, the program might not correctly track the highest and lowest marks entered, leading to inaccurate outputs for maximum and minimum values. For instance, if the code to update maxMark and minMark is not placed correctly within the loop, it may incorrectly assign these variables values that do not reflect the actual input data. Similarly, failing to update minMark or maxMark in every appropriate condition can result in keeping an initial or an earlier mark as the maximum or minimum value, rather than the actual extremes of the entered data set.

Accurately calculating the total number of marks and their sum before computing the average mark is crucial because the average is determined by dividing the total sum of marks by the count of marks entered. If either the sum or the count is incorrect, the computed average will be inaccurate, representing a misleading central value of the dataset. This could lead to erroneous conclusions regarding the overall performance represented by the marks.

The code for finding maximum, minimum, and average exam marks uses the input of -1 to signal the end of input. This value is checked in a while loop condition that continues to execute as long as the input is not -1. When -1 is entered, the loop terminates, and the program proceeds to calculate and print the maximum, minimum, and average marks. This approach allows the program to process a variable number of marks until the user decides to stop by entering -1.

When simulating the throw of a die, several underlying assumptions are made: the die is fair and unbiased, meaning each outcome (1 through 6) has an equal probability of occurring (1/6). This uniformity must be reflected in the simulation algorithm, which would typically use functions such as random.randint(1,6) to mimic real die behavior. If these assumptions aren't met—if, for instance, the random number generation is biased due to a bug or the algorithm does not cover the full range uniformly—it would skew the frequency of each number, leading to incorrect output, such as an unexpected distribution of sixes.

You might also like