Home > Software design >  How do I write input to files in one line and store multiple inputs to the file and be able to read
How do I write input to files in one line and store multiple inputs to the file and be able to read

Time:10-15

I'm a self taught programmer and im trying to make a ticketing system in Python where it accepts multiple inputs and reads from the file depending on the number of tickets. However, the previous inputs get overwritten by the newer inputs and I can't seem to fix it.

The output I get is like this:

J
a
k
e
25
M
a
l
e

But I'd like for the output to look like this:

Jake;25;Male

I've attached the code of this program below. Any help would be greatly appreciated. Thank you.

import sys, select, os
from os import system

def option_1():

    with open(input("Input file name with extension: "), 'w ') as f:

        people = int(input("\nHow many tickets: "))
        name_l = []
        age_l = []
        sex_l = []  

        for p in range(people):
            name = str(input("\nName: "))
            name_l.append(name)
            age = int(input("\nAge: "))
            age_l.append(age)
            sex = str(input("\nGender: "))
            sex_l.append(sex)

        f.flush()
        for item in name:
            f.write("%s\n" %item)
        for item in [age]:
            f.write("%s\n" %item)
        for item in sex:
            f.write("%s\n" %item)


    x=0
    print("\nTotal Ticket: ", people, '\n')
    for p in range(1, people   1):
        print("Ticket No: ", p)
        print("Name: ", name)
        print("Age: ", age)
        print("Sex: ", sex)
        x  = 1



def option_2():

    with open(input('Input file name with extension: '), 'r') as f:
        fileDir = os.path.dirname(os.path.realpath('__file__'))
        f.flush()
        f_contents = f.read()
        print("\n")
        print(f_contents, end = '')

def main():

    system('cls')
    print("\nTicket Booking System\n")
    print("\n1. Ticket Reservation")
    print("\n2. Read")
    print("\n0. Exit Menu")
    print('\n') 

    while True:

        option = int(input("Choose an option: "))
        if option < 0 or option > 2:
            print("Please choose a number according to the menu!")

        else:

            while True:

                if option == 1:
                    system('cls')
                    option_1()
                    user_input=input("Press ENTER to return to main menu: \n")
                    if((not user_input) or (int(user_input)<=0)):
                        main()

                elif option == 2:       
                    system('cls')
                    option_2()
                    user_input=input("Press ENTER to return to main menu: \n")
                    if((not user_input) or (int(user_input)<=0)):
                        main()

                else:
                    exit()


if __name__ == "__main__":
    main()

CodePudding user response:

If you have a recent version of python you can use an f-string to compose the format you require.

You need a loop to iterate over the information you have collected.

You may just need this:

...
f.flush()
for name,age,sex in zip(name_l, age_l, sex_l):
    f.write(f"{name};{age};{sex}\n")
...

Also, the printout to the console needs a similar loop:

print("\nTotal Ticket: ", people, '\n')
for p,(name,age,sex) in enumerate(zip(name_l, age_l, sex_l), start = 1):
    print("Ticket No: ", p)
    print("Name: ", name)
    print("Age: ", age)
    print("Sex: ", sex)

  • Related