0

Code

file=open(r"C:\Users\Owner\Documents\Python\Apparel store\Python\Practical File\File Handling\student.txt","w")
n=1
while n=="1":
    l=[]
    rollno=int(input("Enter rollno:"))
    l.append(rollno)
    name=input("Enter name:")
    l.append(name)
    file.writelines(l)
    n=int(input("Enter 1 to add more record(s):")
file.close()

Output
RESTART: C:\Users\Owner\Documents\Python\Apparel store\Python\Practical File\File_handling.py


**Issue**
*I cannot enter list into text files using write mode, I dont receive any errors, just the above output.*
*I have noticed that the output does not include* `student.txt` *in the directory of the output, how do I solve this?*
`RESTART: C:\Users\Owner\Documents\Python\Apparel store\Python\Practical File\File_handling.py`
  • `1 != "1"` How does the while loop ever end? – Flair Nov 04 '20 at 17:56
  • @Flair you meant it will never enter the loop. – Suthiro Nov 04 '20 at 18:00
  • @Flair you mean start? – Tomerikoo Nov 04 '20 at 18:00
  • @Flair The while loop continues for as long as the user inputs 'n' as 1 (The program asks the user to type in n value at the very end of the program). Users can enter other numbers (1,2 etc) to end the program – I appreciate your help Nov 04 '20 at 18:17
  • Yes I mean start. @I appreciate your help. As others have pointed out, `n` is of type int given your code. But the while loop checks for a string of 1, so it will never start. Even if the line was `n = "1"`, `n=int(input("Enter 1 to add more record(s):")` is of type int, so the loop would end immediately. I was asking a question in hoping you would realize this yourself. – Flair Nov 04 '20 at 18:25

2 Answers2

0

your while loop is not working as you are checking if n is '1' or not, while n refers to the integer object. You file just opens and closes. Try by changing line 3 to while n==1:

Qamar Abbas
  • 176
  • 7
0

You are missing a closing parenthesis in the n=int(input("Enter 1 to add more record(s):") and comparing an int and a str in while.
This works:

file=open(r"C:\Users\Suthiro\student.txt","w")
n=1
while n==1:
    l=[]
    rollno=str(int(input("Enter rollno:")))
    l.append(rollno)
    name=input("Enter name:")
    l.append(name)
    file.writelines(l)
    n=int(input("Enter 1 to add more record(s):"))
file.close()
Suthiro
  • 1,210
  • 1
  • 7
  • 18