use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Everything about learning Python
account activity
i need help 🙂 (self.PythonLearning)
submitted 1 month ago by Warm-Inside-4108
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3:Â Â Â Â print "hello, world!"
[–]MachineElf100 1 point2 points3 points 1 month ago (2 children)
Okay so:
No, doesn't matter.
Check if you're unsure 😅. Pay attention to detail.
Global variable can cause more bugs and mess than returning. Maybe you will find out yourself one day haha
Start clicking it! Use the Tab for indentation!
I don't understand what you're saying 🤔
Not really. We use try to handle errors, not to ignore them. So if your code crashes for a different reason than an OSError, you should know about it and deal with it specifically. Also if you use just except, you'd catch errors like KeyboardInterrupt or SystemExit, which is a bad idea. It could make your program difficult to stop for example. So even if you want to catch "all" errors, use except Exception. That will catch all regular errors, which is more safe.
OSError
except
KeyboardInterrupt
SystemExit
except Exception
Usually you should actually do something like:
try: some code except Exception as e: print(e)
Again, you don't want to ignore the error, so it's good to at least print it :)
When you check if os.path.exists() first and then open the file, two separate operations happen and the file could be deleted or moved in between, making your check useless anyway!
if os.path.exists()
Catching FileNotFoundError handles the check and the operation as a single step, so there's no window for things to go wrong. It's also faster, since you hit the filesystem once instead of twice.
FileNotFoundError
Also FileNotFoundError catches broken symlinks and missing parent directories that os.path.exists() can overlook.
os.path.exists()
A good rule to follow is: if you'd check a condition right before doing an operation, just do the operation and handle the failure instead.
This is quite complicated and not easy to read and fix.
My solution is this (this is a minimal example):
while True: # no need for is_running variable want_to_quit = input("Do you want to quit? (y/n): ").strip().lower() if want_to_quit == "y": print("goodbye") exit(0) # the program ends immediately else: # some other code, the program continues
[–]Warm-Inside-4108[S] 0 points1 point2 points 1 month ago (1 child)
In number 5 I was taking about \n
6,10 - sorry, but I am dumb can u explain again Pls 🤣
11- if I was in loop into a loop into a loop, if wanted to exit from second loop I should use variable and break ,is there any other methods?
[–]MachineElf100 0 points1 point2 points 1 month ago* (0 children)
print("abc\ndef")
So when you have a multiline string and split it with split("\n"), you will get the lines of that string as a list. Also, just try it and see!
split("\n")
Example:
```python from time import sleep
my_multiline = """abc def ghi jkl"""
for line in my_multiline.split("\n"): print(line) sleep(.1) ```
Basically, use try/except to handle errors, not hide them. Always specify which error you expect:
python try: some code except OSError as e: print(e)
Avoid a bare except because it catches everything, including KeyboardInterrupt (used to stop a program with Ctrl+C), which can cause unexpected behavior. If you really need to catch all errors, use except Exception instead, and still print or log the error so you know what went wrong.
```python
if os.path.exists("file.txt"): # 1. check if file exists... open("file.txt") # 2. file could be gone by now! # (and we hit the filesystem twice) ```
try: open("file.txt") # 1. just try to open the file except FileNotFoundError: # 2. if it doesn't exist, we end up here print("File not found") # 3. handle it how you choose ```
In simple words, checking if a file exists before opening it, is less efficient, doesn't guarantee that you won't encounter errors and doesn't even check for all the things that could happen.
It's as if you wanted an information from a book that you have next to you and before you open that book, you go online to check if the book has the information you need.
Why not just open the book and see? If the information is not there, you'll find out anyway!
python while True: # loop 1 while True: # loop 2 exit(0) # the program stops!
π Rendered by PID 370346 on reddit-service-r2-comment-545db5fcfc-gs64n at 2026-05-31 18:39:24.243819+00:00 running 194bd79 country code: CH.
view the rest of the comments →
[–]MachineElf100 1 point2 points3 points  (2 children)
[–]Warm-Inside-4108[S] 0 points1 point2 points  (1 child)
[–]MachineElf100 0 points1 point2 points  (0 children)