help with how to write to file in python using user input to write to .txt | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

help with how to write to file in python using user input to write to .txt

def add_student(): (sName) = input("ID: ") (sMajor) = input("Major: ") (gPa) = input("GPA: ") students_file = open ('students.txt', 'a') students_file.write(sName) students_file.write(sMajor) students_file.write(gPa) students_file.close() this is what I have I'm writing a program that writes input to a students.txt file but I'm having trouble in the txt file it writes everything together I can get it to skip lines if I hard code the input in the students_file.write("413052"+"\n"+"Biology" etc) but as you can see above I need it to be the users input that writes to the file not hard coded and Ive been trying different things I thought that students_file.writlines(sName+"\n"+sMajor+"\n) would work but it doesn't can anybody give me some advice or help?

5th Mar 2021, 8:14 PM
Gutz_X_73vEn
Gutz_X_73vEn - avatar
12 Answers
+ 2
thanks abhay! youre the best(:
5th Mar 2021, 8:38 PM
Gutz_X_73vEn
Gutz_X_73vEn - avatar
+ 2
nice Abhay that worked so now the txt file gets written to correctly so I have another question now the records.py file is the one above with the add_student It is where my functions live that I will call into my admissions.py so I have a program file that's for the main control its my admissions.py it has a main function and a loop. inside of the loop I have choices for the menu like this import records def main(): while True: print("\n\nADMISSIONS") print("-----------") print("1. Add Student") print("2. Update Major") print("3. View All") print("4. Quit") (choice)=input(">Enter Option: ") if choice in "1": records.add_student() <<< this function is working properly elif choice in "2": print("Meow") << don't mind this I haven't created the function yet lol elif choice in "3": read_student() << working on this now elif choice in "4": quit("Goodbye") elif choice not in["1","2","3","4"]: print("Invalid Option Valid Entries Include: 1, 2, 3 or 4") main() main() so you see up there where if choice in 3 is? that's to call my function for reading the students.txt file inside of my records.py the function looks like this def read_student(): word_file = open ('students.txt', 'r') students_file = word_file.read() word_file.close() print(students_file) when I run my program and I select 3 for view all I get a list from the text file that looks like this ['input from sName\n, ','input from sMajor/n, 'input from gpa\n'] what I need is for this to print the same way that its written in the students.txt file like this (what ever input is for) sName sMajor gPa where they appear on new lines and stripped from the special characters of quotes commas and newlines does that make sense?
5th Mar 2021, 9:42 PM
Gutz_X_73vEn
Gutz_X_73vEn - avatar
+ 2
Nevermind I figured it out by making my function like this def read_student(): with open ('students.txt', 'r') as f: f_contents = f.read() print(f_contents)
5th Mar 2021, 10:04 PM
Gutz_X_73vEn
Gutz_X_73vEn - avatar
+ 2
@Abhay I have another question now :( haha in my main loop but I'm having a little trouble trying to figure out how to make it so that I can call the function and input the data then the program ask if it was entered correctly and if so then append but have a choice for no if it wasn't entered correctly? if choice in "1": records.add_student() user_choice = input ('''Enter 'Y' to confirm, 'N' to cancel: ''').lower() if user_choice == "y": print("\n-- Student was added to the database") main() elif input == "n": print("\n-- Entry cancelled") here's a snippet of my function that lives in my records.py def add_student(): (sName) = input ("ID: ") (sMajor) = input("Major: ") (gPa) = input("GPA: ") students_file = open ('students.txt', 'a') students_file.writelines(["\n>\n"+sName+"\n",sMajor+"\n",gPa+"\n"+">\n"]) students_file.close() im not exactly sure because in order to prompt the user I have to call the function but as soon as they enter it automatically appends to the students.txt file but I need a way to confirm before it appends or else loop back to my main
5th Mar 2021, 11:46 PM
Gutz_X_73vEn
Gutz_X_73vEn - avatar
+ 2
perhaps adding it to the function instead of in the main loop like this? def add_student(): (sName) = input ("ID: ") (sMajor) = input("Major: ") (gPa) = input("GPA: ") user_choice = input ('''Enter 'Y' to confirm, 'N' to cancel: ''').lower() if user_choice == "y": students_file = open ('students.txt', 'a') students_file.writelines(["\n>\n"+sName+"\n",sMajor+"\n",gPa+"\n"+">\n"]) print("\n-- Student was added to the database") student_file.close() admissions.main() elif input == "n": print("\n-- Entry cancelled")
6th Mar 2021, 12:03 AM
Gutz_X_73vEn
Gutz_X_73vEn - avatar
+ 2
Okay I figured it out def add_student(): (sName) = input ("ID: ") (sMajor) = input("Major: ") (gPa) = input("GPA: ") user_choice = input ('''Enter 'Y' to confirm, 'N' to cancel: ''').lower() if user_choice == "y": students_file = open ('students.txt', 'a') students_file.writelines(["\n>\n"+sName+"\n",sMajor+"\n",gPa+"\n"+">\n"]) print("\n-- Student was added to the database") students_file.close() else: if user_choice == "n": print('\n-- Data entry cancelled. Re-enter Student Data') add_student() I let all of the logic live within the function and then I only let the main control loop call the function :P
6th Mar 2021, 12:23 AM
Gutz_X_73vEn
Gutz_X_73vEn - avatar
+ 1
Gutz_X_73vEn students_file.write(sName+"\n") Or students_file.writelines([sName+"\n",sMajor+"\n",gPa+"\n"])
5th Mar 2021, 8:34 PM
Abhay
Abhay - avatar
+ 1
YAY:D hahaha
5th Mar 2021, 10:05 PM
Gutz_X_73vEn
Gutz_X_73vEn - avatar
+ 1
@abhay oh sweet thats good to know:D now i can summon the great Abhay when i get into trouble! haha jk
5th Mar 2021, 10:39 PM
Gutz_X_73vEn
Gutz_X_73vEn - avatar
+ 1
Gutz_X_73vEn use right name though!!. And i am learning as well not like i am a professional developer but i do help when i can!
5th Mar 2021, 11:46 PM
Abhay
Abhay - avatar
+ 1
Abhay haha oops! youre doing great youre the most helpful person i know! theres just soo much to learn:0
5th Mar 2021, 11:47 PM
Gutz_X_73vEn
Gutz_X_73vEn - avatar
0
Gutz_X_73vEn you can use @ followed by Person name to mention someone .
5th Mar 2021, 10:28 PM
Abhay
Abhay - avatar