0% found this document useful (0 votes)
8 views

Python code for 15 marker questions[1]

The document outlines a programming assignment for Grade IX students at Heartfulness International School, focusing on creating a program to manage player scores in a sports tournament. It includes pseudocode requirements for inputting player names and scores, calculating total and average scores, and identifying the highest and lowest scoring players. Additionally, it provides Python code that implements these requirements, along with explanations of key functions used in the code.

Uploaded by

rohprak88
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Python code for 15 marker questions[1]

The document outlines a programming assignment for Grade IX students at Heartfulness International School, focusing on creating a program to manage player scores in a sports tournament. It includes pseudocode requirements for inputting player names and scores, calculating total and average scores, and identifying the highest and lowest scoring players. Additionally, it provides Python code that implements these requirements, along with explanations of key functions used in the code.

Uploaded by

rohprak88
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

HEARTFULNESS INTERNATIONAL SCHOOL, OMEGA BRANCH 2024 - 25

GRADE: IX CHAPTER 8 : PROGRAMMING

15 marker questions: Write pseudocode for the following scenarios:


Question 1:

A one-dimensional (1D) array Players[] contains the names of up to 12 players in a sports


tournament.
A two-dimensional (2D) array Scores[] is used to store the scores obtained in three rounds for
each player:

• Round 1
• Round 2
• Round 3

For example, Scores[15, 18, 20] denotes:

• Score in Round 1 for Player1 = 15,


• Score in Round 2 for Player1 = 18,
• Score in Round 3 for Player1 = 20.

The position of any player's data in the Scores[] array corresponds to the same index in
Players[].
For example, the data in index 4 of Scores[] belongs to the player in index 4 of Players[].

The variable TotalPlayers stores the number of players participating in the tournament, which
must be at least 4 but no more than 12.

Write a program that meets the following requirements:

1. Allows the number of players (TotalPlayers) to be input, stored, and validated.


2. Allows the name of each player and the scores for all three rounds to be entered and
stored.
3. Calculates the total score for each player as the sum of the scores from the three rounds
and stores it.
4. Calculates the average score for all players, rounded to two decimal places.
5. Finds the player with the highest total score and the player with the lowest total score.
6. Outputs the names of all players with their respective round scores and total score.
7. Outputs the name of the player with the highest and lowest total scores.
8. Outputs the overall total score and the average score across all players.
HEARTFULNESS INTERNATIONAL SCHOOL, OMEGA BRANCH 2024 - 25

PYTHON CODE 1

# Step 1: Input and validate TotalPlayers

while True:

TotalPlayers = int(input("Enter the number of players (between 4 and 12): "))

if 4 <= TotalPlayers <= 12:

break

else:

print("Invalid input! Please enter a number between 4 and 12.")

# Step 2: Initialize lists to store player names, their scores for 3 rounds, and total scores for each player

Players = []

i=0

while i < TotalPlayers:

Players.append("") # Add an empty string for each player

i += 1

Scores = []

i=0

while i < TotalPlayers:

Scores.append([0] * 3) # Create a list of three zeros for each player (3 rounds)

i += 1

TotalScores = []

i=0

while i < TotalPlayers:

TotalScores.append(0) # Initialize total score to 0 for each player

i += 1
HEARTFULNESS INTERNATIONAL SCHOOL, OMEGA BRANCH 2024 - 25

# Input player names and scores

for i in range(TotalPlayers):

# Prompt for player name and store it

Name = input(f"Enter the name of Player {i + 1}: ")

Players.append(Name)

# Initialize empty list for player scores and total score for the current player

PlayerScores = []

Total = 0

# Loop through 3 rounds to collect scores for each player

for j in range(3):

# Input score for the current round

Score = int(input(f"Enter score for Round {j + 1} for {Name}: "))

# Validate the score to ensure it's non-negative

while Score < 0:

print("Invalid input. Please enter a non-negative number.")

Score = int(input(f"Enter score for Round {j + 1} for {Name}: "))

# Add score to player's scores and update total score

PlayerScores.append(Score)

Total += Score

# Store the player's scores and total score in the corresponding lists

Scores.append(PlayerScores)

TotalScores.append(Total)
HEARTFULNESS INTERNATIONAL SCHOOL, OMEGA BRANCH 2024 - 25

# Input player names and scores

for i in range(TotalPlayers):

# Prompt for player name and store it

Name = input(f"Enter the name of Player {i + 1}: ")

Players.append(Name)

# Initialize empty list for player scores and total score for the current player

PlayerScores = []

Total = 0

# Loop through 3 rounds to collect scores for each player

for j in range(3):

# Input score for the current round

Score = int(input(f"Enter score for Round {j + 1} for {Name}: "))

# Validate the score to ensure it's non-negative

while Score < 0:

print("Invalid input. Please enter a non-negative number.")

Score = int(input(f"Enter score for Round {j + 1} for {Name}: "))

# Add score to player's scores and update total score

PlayerScores.append(Score)

Total += Score

# Store the player's scores and total score in the corresponding lists

Scores.append(PlayerScores)

TotalScores.append(Total)
HEARTFULNESS INTERNATIONAL SCHOOL, OMEGA BRANCH 2024 - 25

# Step 3: Calculate total and average scores

TotalSum = sum(TotalScores) # Sum of all total scores

AverageScore = round(TotalSum / TotalPlayers, 2) # Calculate and round average score

HighestScore = max(TotalScores) # Find the highest total score

LowestScore = min(TotalScores) # Find the lowest total score

HighestIndex = TotalScores.index(HighestScore) # Index of the player with highest score

LowestIndex = TotalScores.index(LowestScore) # Index of the player with lowest score

# Step 4: Output player details

print("\nTournament Results:")

for i in range(TotalPlayers):

print(f"{Players[i]} Scores: {Scores[i][0]}, {Scores[i][1]}, {Scores[i][2]} | Total: {TotalScores[i]}")

# Step 5: Output highest and lowest scoring players

print(f"\nPlayer with the highest score: {Players[HighestIndex]} ({HighestScore})")

print(f"Player with the lowest score: {Players[LowestIndex]} ({LowestScore})")

# Step 6: Output total and average scores

print(f"\nOverall Total Score: {TotalSum}")

print(f"Average Score across all players: {AverageScore}")

KEY NOTES

The f in the code is used for f-strings (formatted string literals) in Python. It allows you to
embed variables directly inside a string by enclosing them in curly braces {}.

For example:

Name = "Alice"
print(f"Hello, {Name}!")

Output:

Hello, Alice!
HEARTFULNESS INTERNATIONAL SCHOOL, OMEGA BRANCH 2024 - 25

This is a cleaner and more readable way to format strings compared to older methods like
.format() or concatenation with +.

Functions sum(), max(), round(), and min() in Python:


1. sum():
o Purpose: This function calculates the sum of all elements in a list).
o Syntax: sum(list, start=0)
o Example:

numbers = [1, 2, 3, 4]
total = sum(numbers) # total will be 10

2. max():
o Purpose: This function returns the largest item from a list or the largest of two or more
arguments.
o Syntax: max(list, *args, key=None, default=None)
o Example:

scores = [85, 92, 78, 90]


highest = max(scores) # highest will be 92

3. round():
o Purpose: This function rounds a number to a specified number of decimal places.
o Syntax: round(number, digits)
o number: The number to round.
o digits: The number of decimal places to round to (optional; default is 0).
o Example:

value = 3.14159
rounded_value = round(value, 2) # rounded_value will be 3.14

4. min():
o Purpose: This function returns the smallest item from a list or the smallest of two or
more arguments.
o Syntax: min(list, *args, key=None, default=None)
o Example:

scores = [85, 92, 78, 90]


lowest = min(scores) # lowest will be 78
HEARTFULNESS INTERNATIONAL SCHOOL, OMEGA BRANCH 2024 - 25

Usage in the code:

• sum(TotalScores): Adds up all the values in the TotalScores list to calculate the total
score of all players.
• max(TotalScores): Finds the highest total score among all players.
• round(TotalSum / TotalPlayers, 2): Calculates the average score and rounds it to 2
decimal places.
• min(TotalScores): Finds the lowest total score among all players.

You might also like