0

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
kkx07805
  • 5
  • 1
  • 1
  • 4

2 Answers2

0

The function that collects the ages def save() would pass the age values by putting the values you want to pass into parentheses of the function you want to pass the values to, same as you did with save(in1,in2,in3), but this time pass them to def average(): by first calling the function:

average(in1, in3, in3)

and return the result, avg variable.

But you'll also need to tell the receiving functiondef average() to accept those 3 parameters as:

def average(in1, in2, in3):

So all together keeping it as close your design as possible:

def main():
    while True:
        with open("ages.txt", "a") as myfile:
            n1, n2, n3 = age()
            avg = average(n1, n2, n3)
            save(n1, n2, n3, avg, myfile)
            if input("Press enter to repeat (or type `stop` to end): " ) == 'stop':
                myfile.close()
                break



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?: "))
    return in1, in2, in3

def average(in1,in2,in3):
    avg = (in1 + in2 + in3) / 3
    return avg

def save(in1,in2,in3,avg, myfile):
    in1 = round(in1,2)
    in2 - round(in2,2)
    in3 - round(in3,2)
    myfile.write("%s\n" % in1)
    myfile.write("%s\n" % in2)
    myfile.write("%s\n" % in3)
    myfile.write("average: %s\n" % str(avg))

if __name__ == '__main__':
    main()
  • Thank you Daved and everyone else! How could I restart the program until the user types "stop"? – kkx07805 Feb 28 '19 at 03:32
  • What does w+ do and how it different from w? – kkx07805 Feb 28 '19 at 03:39
  • I ran the program and it said break was outside the loop. – kkx07805 Feb 28 '19 at 03:41
  • 'w+' will clear the file and rewrite new contents, change to 'a' if you want to keep the contents each time you run your program, see here: https://stackoverflow.com/questions/4706499/how-do-you-append-to-a-file-in-python – chickity china chinese chicken Feb 28 '19 at 03:48
  • You're right I forgot the while loop, Thanks for your feedback. it should work now. change back to open("ages.txt", "a") if you don't want to delete the ages after each program run. see all the options here: [python open built-in function: difference between modes a, a+, w, w+, and r+?](https://stackoverflow.com/a/1466036/1248974), cheers! – chickity china chinese chicken Feb 28 '19 at 06:27
0

You can create endless loop and asks user to break it:

with open('ages.txt', 'a+') as f:
    while True:
        ages = []
        for i in range(3):
            ages.append(int(input('How old is the person {}?: '.format(i+1))))
        average = 'Average: {}'.format(round(sum(ages) / len(ages), 2))
        print(average)
        f.write('{}\n{}'.format('\n'.join([str(x) for x in ages]), average))
        action = input('Press [1] to continue or [2] to exit... ')
        if action == '2':
            break

Example output:

How old is the person 1?: 18
How old is the person 2?: 25
How old is the person 3?: 44
Average: 29.0
Press [1] to continue or [2] to exit... 1
How old is the person 1?: 77
How old is the person 2?: 32
How old is the person 3?: 100
Average: 69.67
Press [1] to continue or [2] to exit... 2

Content of ages.txt:

18
25
44
Average: 29.0
77
32
100
Average: 69.67
Alderven
  • 7,569
  • 5
  • 26
  • 38