I am writing a series of numbers to a text file using a sentinel. If the user enters -1
it will end the program. I added in another loop for an if statement, and it is causing the first input the user makes to not print.
I could input: 1,2,3,4,5,-1
The text file would read: 2,3,4,5,-1
Here is my code:
outfile = open("userInput.txt","w")
userInput = int(input("Enter a number to the text file: "))
count = 0
while int(userInput) != -1:
userInput = int(input("Enter a number to the text file: "))
outfile.write(str(userInput) + "\n")
count +=1
if(count) == 0:
print("There is no numbers in the text file")
outfile.write("There is no numbers in the text file")
outfile.close()
The while loop worked fine. When I made the count
variable, count+=1
, and the if statement then the writing problem occurred.
I made it so that if the user just enters no values it would say there's nothing in the text file and writes it to the file as well.