Warning! It seems that you are using Dodona within another webpage, so not everything may work properly. Let your teacher know so that he can solve the problem by adjusting a setting in the learning environment. In the meantime, you can click this link to open Dodona in a new window.
Warning! The page was not fully loaded, probably because of a network issue. It could be that not all functionalities work as expected. Please try reloading the page.
Python exercise
Rainfall collector
Sign in to hand in your solution and get feedback.
# Rainfall collector program
# -------------------------
# Subprograms
# -------------------------
def read_data(filename):
num_regions = 7
data = [["" for columns in range(2)] for rows in range(num_regions)]
file = open(filename, "r")
for region in range(num_regions):
data_row = file.readline()
data_row = data_row.strip()
data_list = data_row.split(",")
data[region][0] = data_list[0]
data[region][1] = data_list[1]
file.close()
return data
def padded_label(label, characters):
return label + " " * (characters - len(label))
def output_table(data):
num_regions = 7
characters = 0
for region in range(num_regions):
if len(data[region][0]) > characters:
characters = len(data[region][0])
for region in range(num_regions):
table_row = padded_label(data[region][0], characters)
table_row = table_row + " | "
table_row = table_row + data[region][1]
print(table_row)
print()
# -------------------------
# Main program
# -------------------------
data = read_data("data.txt")
output_table(data)