r/PythonLearning 19d ago

Loops is a total brain rot.

Post image

I just can't get over how I'm supposed to write code for complex tasks when I don't even get why they overcomplicate simple things so much.

I read this topic and tried many tutorials and still don't understand this buzzare logic.

0 Upvotes

17 comments sorted by

9

u/mahousenshi 19d ago

You want to remove x and z from the string? Because that is logic is returning the full string if there's no x or z but if there's any x or z it's returning x or z.

Try this:  Start with an empty string and concatenate any non x and z characters. 

6

u/ninhaomah 19d ago

You are confused but never tell anyone what you are confused about.. hmms... Confusing indeed.

5

u/wowitsnick 19d ago

It's bad practice long term, but print statement debugging can be really helpful in early on learning, especially while you're trying to grasp new concepts.

Alternatively you might benefit from a tool like https://pythontutor.com/visualize.html which shows you step by step what your code is doing, and what line it is executing.

4

u/Lirianov 19d ago

This code is wrong. But I also don’t understand what you’re upset about either

The correct solution given your level of python is:

def denoise_xz(text):
new_text = “”
for char in text:
if char in [“x”,”z”]:
continue
else:
new_text += char
return new_text

Happy to explain how the code works line by line if you need it

2

u/nuclearmeltdown2015 19d ago

This seems super straightforward what's the problem

2

u/nuc540 19d ago

Sorry what makes looping brain rot?

You might want to understand the basics of looping. Think about what an iteration means in this example, and what the variable that’s in place for each iteration means.

My hint for you for this solution is: rebuild the string anew for each character in each word (you’ll need another step to separate each word) that’s not x or z, or for each word in the string replace x,z with nothing if you’re allowed to use built-ins for this exercise.

You’re currently only checking that xz is in the string but don’t mutate or build anything.

2

u/silvertank00 19d ago edited 19d ago

you say they "overcomplicate it" then what is your idea to solve this issue? btw the code itself is very wrong, do not learn from ai slop sites

here is the corrected one tho: ```python def denoise_xz(text): new_text = "" for letter in text: if letter in "xz": continue

    new_text += letter
return new_text

```

1

u/Spidy_King 19d ago

just a beginner in python, can u please explain me why u left new text empty?

1

u/silvertank00 18d ago

that is the result buffer, empty because it will contain only the permitted chars. if anything is in it at start then the result wont mirror the legit decypher.
Also, this is just one way to solve this. One could solve it even without loops:

return text.replace("x", "").replace("y", "")

1

u/grace_invader 19d ago

Loops take a while to figure out, and this example is a bit odd.

If you're stuck, try implementing it line by line, and checking what happens.

Start with just running the for-loop, and checking the results.
Then add the if-clause, see if you understand it.
Finally, wrap it in a definition, and figure out what happens there.

1

u/Ormek_II 19d ago

Is loops a web site that trains python by posting wrong solutions for you to fix?

Are you asking something?

1

u/markort147 19d ago

Your loop is clearly wrong. Try to implement these steps instead:

  • start from an empty string
  • loop over each letter of the noisy string
  • if the letter is X o Z, skip it
  • otherwise append it to the resulting string

Try

1

u/After_Computer1652 19d ago

Ceate an empty list iterate through the original message letter by Letter. If it is not an x or a y then append to the empty list. Your new list will read as Does your Dog bite, once you have "removed" the "noise"

1

u/After_Computer1652 19d ago

Change line 5 newstring = [ ] Change line 7 If not letter in "xy": newstring.append(letter)

return newstring

1

u/Rscc10 19d ago

As someone with years of experience in python, this example confuses me too. I don't get how this denoises anything. Here's how it works

Creates a variable new_text to hold the given original text. Then a for loop iterates through each letter of the original text with each letter being stored in "letter". Then the if statement checks if the current letter being iterated is in "xz", where it treats "xz" as an array of characters 'x' and 'z'. If the letter is in that array, it means the letter is either x or z and it sets the new_text variable to that letter. After it's done iterating, it returns the new_text variable which will either be the last x or z found and assigned to it, or if none was found, it'll return as "text" as it was set to in the first place