0 10 20 30 40 50 60 70 80 90 100 100124 0
# ------------------------------------------------------------------- # Modules # ------------------------------------------------------------------- import random # ------------------------------------------------------------------- # Constants # ------------------------------------------------------------------- COUNTRIES = ["Great Britain", "United States", "China", "Norway"] FILENAME = "medals.csv" # ------------------------------------------------------------------- # Global variables # ------------------------------------------------------------------- number_of_countries = 0 gold_medals = [] silver_medals = [] bronze_medals = [] file = None # file to write to i = 0 # ------------------------------------------------------------------- # Subprograms # ------------------------------------------------------------------- def generate_medal_data(pNumberOfCountries): for i in range(pNumberOfCountries): gold = random.randint(0, 50) gold_medals.append(gold) silver = random.randint(0, 50) silver_medals.append(silver) bronze = random.randint(0, 50) bronze_medals.append(bronze) # ------------------------------------------------------------------- # Main program # ------------------------------------------------------------------- number_of_countries = len(COUNTRIES) generate_medal_data(number_of_countries) print("Writing medal data to " + FILENAME) file = open(FILENAME, "w") file.write("Country,Gold,Silver,Bronze\n") # iterate through every country for i in range(number_of_countries): file.write(COUNTRIES[i] + ",") file.write(str(gold_medals[i]) + ",") file.write(str(silver_medals[i]) + ",") file.write(str(bronze_medals[i]) + "\n") file.close() # Challenges: # 1) Calculate the total number of medals for each country # 2) Write the total number of medals for each country to the CSV file # 3) Display the overall number of medals to the screen
You are not logged in
`¬
1!
2"
3£
4$
5%
6^
7&
8*
9(
0)
-_
=+
TAB
Q
W
E
R
T
Y
U
I
O
P
[{
]}
ENTER
CAPS
A
S
D
F
G
H
J
K
L
;:
'@
#~
ENTER
SHIFT
\|
Z
X
C
V
B
N
M
,<
.>
/?
SHIFT
 
SPACE
 
0 WPM
0%
0s
0%