I'm trying to calculate and print the ages of three people. First I want to create a main function that opens a text file called "ages.txt". Then I want the main function to call: a function that asks the user for the ages of three people, a function that calculates the average of those ages, and a function that writes the ages and the average that has been rounded to two decimal places to a text file called "ages.txt" and then closes the file. Then the main function should reopen the file to append more data. The main function should repeat this process over until the user tells the program to stop.Right now I'm not sure how to pass data from the function that collects the ages. How can I pass the data from the different functions?
def main():
with open("ages.txt", "w") as myfile:
def age():
in1 = int(input("How old is the first person?: "))
in2 = int(input("How old is the second person?: "))
in3 = int(input("How old is the third person?: "))
def average(in1,in2,in3):
avg = (in1 + in2 + in3) / 3
def save(in1,in2,in3,avg):
in1 = round(in1,2)
in2 - round(in2,2)
in3 - round(in3,2)
myfile.write(in1 + "\n")
myfile.write(in2 + "\n")
myfile.write(in3 + "\n")
myfile.write("average:" + avg + "\n")
I want the text file that the program creates to look something like this:
8
10
9
Average: 9
15
16
16
Average: 15.67
22
14
18
Average: 18