Python code for 15 marker questions[1]
Python code for 15 marker questions[1]
• Round 1
• Round 2
• Round 3
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.
PYTHON CODE 1
while True:
break
else:
# Step 2: Initialize lists to store player names, their scores for 3 rounds, and total scores for each player
Players = []
i=0
i += 1
Scores = []
i=0
i += 1
TotalScores = []
i=0
i += 1
HEARTFULNESS INTERNATIONAL SCHOOL, OMEGA BRANCH 2024 - 25
for i in range(TotalPlayers):
Players.append(Name)
# Initialize empty list for player scores and total score for the current player
PlayerScores = []
Total = 0
for j in range(3):
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
for i in range(TotalPlayers):
Players.append(Name)
# Initialize empty list for player scores and total score for the current player
PlayerScores = []
Total = 0
for j in range(3):
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
print("\nTournament Results:")
for i in range(TotalPlayers):
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 +.
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:
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:
• 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.