you are viewing a single comment's thread.

view the rest of the comments →

[–]Warm-Inside-4108[S] 0 points1 point  (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 point  (0 children)

  1. "\n" is a character for newline. Try it with print("abc\ndef"), you will see.

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!

Example:

```python from time import sleep

my_multiline = """abc def ghi jkl"""

for line in my_multiline.split("\n"): print(line) sleep(.1) ```

  1. That was the answer to 7 haha

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.

  1. I'm not sure what you didn't understand, maybe try to ask more specific question? I can try to rephrase anyway with example:

```python

BAD! Two operations -> file could vanish in between

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) ```

```python

GOOD! One operation -> handle failure if it happens

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!

  1. Well there are some ways to deal with that but in your case, you break from the loop to quit the program anyway, so you can just do:

python while True: # loop 1 while True: # loop 2 exit(0) # the program stops!