dimanche 1 octobre 2017

If statements to find age in a list

I have made three inputs (firstname, surname and age) and put them into a file. The last part is to use this information to print out the inputs of someone if the age is above 18. I have tried using a dictionary but I did it wrong somehow. Can I use a list or a file? What can I do in order to use an if statement using that age to find out whether the details should be printed or not. Sample below.

def details():
    c = input('Press 1 to enter details, press 2 to browse users or press 3 to check age')
    if c == '1':
        firstname = input('Please enter your firstname')
        surname = input('Please enter your surname')
        age = input('Please enter your age')
        myfile=open('details.txt', 'at')
        myfile.write(firstname + '\n')
        myfile.write(surname + '\n')
        myfile.write(age + '\n')
        myfile.close()
        detail2 = {'Fname': firstname, 'Sname': surname, 'Age': age}
        details()
    elif c == '2':
        detail2 = {'Fname': firstname, 'Sname': surname, 'Age': age}
        read()
    elif c == '3':
        detail2 = {'Fname': firstname, 'Sname': surname, 'Age': age}
        read18()
    else:
        details()
def read():
    myfile=open('details.txt', 'rt')
    x = myfile.read()
    print(x)
def read18():
    for item in detail2:
        if Age> 18:
            print('Over 18') 
        elif Age< 18:
            print('Under 18')
"""I need to know what to do so this will print the details. Just using over or under 18 as a starting point"""

details()

Aucun commentaire:

Enregistrer un commentaire