0

If the User Input in mark() funtion is string not integer the list.txt content will delete and if it's true also it will delete the content then after i want to return to main() funtion

Example:

Enter a number to mark: g
Invalid Input!//the list.txt file content here was deleted

Enter a number to mark: 2 //true but the content of the list.txt was deleted also

and Return to the Main if true

How to fix this Error?

def mark(pick):
   pick = fileName
   flag = open(pick, "w")
   while True:
      choice = input("Enter number to mark: ")
      if choice == True:
         return True
         break
      list[int(choice)] = list[int(choice)].replace('"N"', '"Y"')
      for line in list:
            flag.write(line)
      try:
         val = int(choice)
         return val
      except ValueError:
         print("Invalid Input")
         return choice

   print(list[int(choice)]," has been mark as visited")
Newbieee
  • 167
  • 10
  • Why do you have a return statement in your error handler? You are also not closing your file, which is sloppy. It would be better to use `with` rather than just `open` – John Coleman Jun 08 '20 at 23:37
  • the error is it will delete the content of the list.txt.file if im going to choose a line to write and also if the user input is error the content of the list.txt will delete i dont know why – Newbieee Jun 08 '20 at 23:39
  • im just a beginner in python and practice this hahaha so much error – Newbieee Jun 08 '20 at 23:41
  • you can help me man?? – Newbieee Jun 09 '20 at 00:08
  • There are a number of problems with your function. I would advise you to work more incrementally; that is, make one small part of the program work before moving on to the next small part. Alternatively, you could construct and post a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). What you have posted will not even compile. – Dennis Sparrow Jun 09 '20 at 04:18

0 Answers0