#๐Ÿ”’ Python not engaging with file, resulting in logic error

69 messages ยท Page 1 of 1 (latest)

lean musk
#

Code in attached file

Output:

Welcome, user!

  1. Enter new entry
  2. Check history
  3. Clear history by category
  4. Close application
    What would you like to do today? Please input a number: 2
  5. Food
  6. Transport
  7. Utilities
  8. Entertainment
  9. Miscellaneous/Other
    Which category would you like to check?: 2

Which category would you like to check?: 1

Which category would you like to check?: 3

Which category would you like to check?: 4

Which category would you like to check?: 5

Which category would you like to check?: 6
Please enter a valid number.
Which category would you like to check?: 0
Please enter a valid number.
Which category would you like to check?: test
Please enter a valid number.
Please enter a valid number.
Which category would you like to check?:

jaunty minnowBOT
#

@lean musk

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

neat heron
#

to debug, print category right after having it assigned

#

I ran your code and it works fine

lean musk
#

thanks

#

i tried making a small if statement that checks the length of the text in my files and print something if theres nothing and it doesnt do anything, even if im checking the file manually and its empty

#

wait how does that work? category is just an input variable to decide which file is interacted with

neat heron
#

something that is wrong in your code is that if the except is triggered, something is printed but you still executed the remaining of the loop

lean musk
lean musk
neat heron
#

well, you can still print it

#

print(category)

#

you don't need to close (I may think it can even be a problem) a file if you open it with with open(...)

lean musk
#

I thought that as well but nothing changed here

#

like I've tried closing it, I've tried not closing it. No change in result

neat heron
#

also while category_check == False

#

ewww

#

while not category_check is better

lean musk
#

didnt know that was a thing

#

ooooookay now VS code is being weird. its trying to start the code halfway through

neat heron
#

and it is even easier to use break and continue in a while True loop

lean musk
#

like i'll click Run and it'll immediately ask the category i wanna check

#

like my dude theres an entire block of code to go through before executing functions

lean musk
neat heron
#

like answercheck

lean musk
#

ive had this issue before and i just restarted VSC and it fixed itself

#

i havent touched answercheck

#

and printing category didnt help, its still not writing to the file or printing its contents

#

okay this is interesting:

File "c:\Users\myname\OneDrive\Documents\python project 2025\Summer Programming Assignment 2025.py", line 30, in list_check
if len(file) == 0:
^^^^^^^^^
TypeError: object of type '_io.TextIOWrapper' has no len()

#

but it does? i manually entered something to check whether the file was reading

#

it just says "test1" in there

neat heron
#

a file has no len

#

you can read what's inside and compute its length, but no the length of a file

lean musk
#

is there a way to check if a file is empty?

neat heron
#

yes, you read it and you check the length of what you read

neat heron
lean musk
#

i mean it just prints the number i inputted and continues with the code

lean musk
neat heron
#

you can read the content of a file with

with open(..., 'r') as file:
  content = file.read()

content is a str with the content of the file

lean musk
#

ahhhhhhhh ok

#

and then print(content)?

neat heron
#

and then whatever you want to do with content

lean musk
#

ok cool thanks

#

what about the writing issue?

#

cuz im entering new entries and nothing happens

neat heron
#

for the files like food etc, you are not writing anything inside

#

for the files like f.txt, g.txt I am not sure, maybe that .close() isn't correct

lean musk
#

possibly

#

ive also found that i had to manually create the files but the variables right at the top of the code are literally there to automatically create the files

neat heron
#

oooo but you never close those files

#

when you open them first

#

that's a problem

#

and no, you don't need it

#

remove those lines

lean musk
#

ok

#

so im correcting the file names. f.txt, etc arent meant to be there, its meant to be the full names (food.txt, entertainment.txt, etc)

#

looks like its all done

#

what about the clearing section?

#

ive got the file writing and reading now

#

i just need it to reset files as well

#

wait hang on

#

its reading a file but i cant find it

#

its not reading the correct file

#

all files are empty yet its still printing something

viscid plume
jaunty minnowBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.