Home > Mobile >  How to add a line of inputted text next column over and over until the person prompts to quit on Pyt
How to add a line of inputted text next column over and over until the person prompts to quit on Pyt

Time:10-26

My code works fine for the salesperson to enter the following criteria until the file is created with one list after being prompted to continue or quit writing the .txt file.

def write_text():
    print("Welcome to a Costco Hotel Text Creator.")
    print('-'*20) #This serves no purpose but to make the terminal look cleaner.
    new_file = input("Enter a new file name: ")
    while True: #In the loop where the sales person must enter the following criteria for the new text.
        with open(new_file, "w") as f:
            f.write(str(input("Enter a member's name: ")))
            f.write(str(";")) #Makes it easier and logical to seperate the text by semicolon.
            print("The members services are: ")
            services = ['Conference','Dinner','Lodging','Membership Renewal'] 
            for service in services:
                print(f"{service.capitalize() : <14}") #Capitalizes the string, but not allowed to type exactly.
            f.write(str(input("Enter the hotel services. Type exactly shown: ")))
            f.write(str(";")) #Makes it easier and logical to seperate the text by semicolon.
            f.write(str(input("Enter the price: ")))
            f.write(str(";")) #Makes it easier and logical to seperate the text by semicolon.
            f.write(str(input("Enter the full date (MM/DD/YYYY): ")))
        print('-'*20) #This serves no purpose but to make the terminal look cleaner.
        exit = input("Are you done writing the text file? If yes, please type 'yes' to exit")
        if exit == 'yes': #If the person typed 'no' then it exits before entering the sales system.
            print("Exiting the saved file.")
            break
write_text()

My result:

Sammy;Conference;20.00;09/25/2022

Expected result:

Sammy;Conference;20.00;09/25/2022
Johnny;Conference;25.00;09/25/2022
Tony;Conference;30.00;09/25/2022

The list goes on until the user can enter 'yes' to quit. I already have the function that allows reading the text file to sum the revenue per category. Any help and advice is appreciated. Thank you!

CodePudding user response:

When you use with open(...) as .. it closes the file as soon as you exit the code block, and since you are using the w file mode you are overwriting your previous entries every time your loop goes back to the top. You also don't need to wrap input statements with str(). The return value of input will always be a string.

One solution would be to simply put the while true loop inside of the the with open( ) block. You will also need to add a line break at the end of the loop.

def write_text():
    print("Welcome to a Costco Hotel Text Creator.")
    print('-'*20)                          # This serves no purpose but to make the terminal look cleaner.
    new_file = input("Enter a new file name: ")
    with open(new_file, "w") as f:
        while True:                        # In the loop where the sales person must enter the      
            f.write(input("Enter a member's name: ")   ";")
            print("The members services are: ")
            services = ['Conference','Dinner','Lodging','Membership Renewal'] 
            for service in services:
                print(f"{service.capitalize() : <14}")   # Capitalizes the string, but not allowed to type exactly.
            f.write(input("Enter the hotel services. Type exactly shown: ")   ";")
            f.write(input("Enter the price: ")   ";")
            f.write(input("Enter the full date (MM/DD/YYYY): ")   "\n")
            print('-'*20)            
            exit = input("Are you done writing the text file? If yes, please type 'yes' to exit")
            if exit == 'yes':      # If the person typed 'no' then it exits before entering the sales system.
               print("Exiting the saved file.")
               break
write_text()

You can also combine the ";" with the text from the input statement to cut back on calls to write. This also makes it look cleaner and more readable as well.

You could also just use the a/append file mode, but closing and reopening the file over and over again doesn't seem like the better option.


Update

If you want to verify that the user input matches a certain word you can do something like this, lets say you want to validate that the user input one of the listed services correctly

print("The members services are: ")
services = ['Conference','Dinner','Lodging','Membership Renewal'] 
for service in services:
    print(f"{service.capitalize() : <14}")   # Capitalizes the string, but not allowed to type exactly.
while True:
    response = input("Enter the hotel services. Type exactly shown: ")
    if response in services:
        f.write(response   ';')
        break
    else:
        print("Incorrect entry please try again.")

I would recommend taking this functionality and putting it into a separate function so that you can apply the logic to all of your input statements.

  • Related