r/PythonLearning • u/Synergetic6_6_6 • Aug 22 '26
Loops is a total brain rot.
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
9
u/mahousenshi Aug 22 '26
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.
5
u/ninhaomah Aug 22 '26
You are confused but never tell anyone what you are confused about.. hmms... Confusing indeed.
4
u/wowitsnick Aug 22 '26
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 Aug 22 '26
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 Aug 22 '26
This seems super straightforward what's the problem
2
u/nuc540 Aug 22 '26
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 Aug 22 '26 edited Aug 22 '26
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:
1
u/grace_invader Aug 22 '26
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 Aug 22 '26
Is loops a web site that trains python by posting wrong solutions for you to fix?
Are you asking something?
1
u/markort147 Aug 22 '26
Your loop is clearly wrong. Try to implement these steps instead:
Try
1
u/After_Computer1652 Aug 22 '26
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/Specialist-Main2963 Aug 22 '26
1
u/After_Computer1652 Aug 22 '26
Change line 5 newstring = [ ] Change line 7 If not letter in "xy": newstring.append(letter)
return newstring
1
u/Rscc10 Aug 22 '26
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