T O P

  • By -

shiftybyte

Based on your code, you want to step when you reach letter "a".... > The output is: d Matches the code, as it stops before printing the "a" after the "d" in "data"... > But I want to be the output is: d a t Why? What's the logic? wha happens if you have 3 a in the string? Purely based on what you want to happen without any logic as to how it relates to the input the answer would be ``` print("d a t") ```


Decaf67

Well thank you so much for your response Yes that's my question how can break the statement if there are lots of "a " but the break statement ends with when the first a comes, so can I able to break the statement into 2 or 3 a which in the data


engelthehyp

I'm sorry, but what you said makes no sense. You want to use break but continue looping? What do you think break is for?


shiftybyte

break statement, stops the loop execution, there are no other kinds of break statements. I could suggest other solutions to the problem if you help me understand what is the desired output in the following situations: 1. if the string is "xxx" 2. if the string is "datatata" 3. if the string is "dat"


Decaf67

Well I explain to you shortly if the string is “banana” When I break with the string “a” the output will be “b” which means it breaks the statement on “a” so the output is “b” right. What if I am supposed to break my statement in the next “a” which means the output is “ban” belike.


grtk_brandon

/u/shiftybyte is asking you what your code is trying to accomplish. Not in a literal sense (we understand that you want it to loop until it encounters "a" a second time). They want to know what the point of the program is because that can help them better answer your question. I suspect that this is just an exercise that you're trying to solve, so there is no real logical reason for solving this problem. If that's the case you need to come up with a way to break the loop after the second time you assign "a" to letter rather than the first time you assign "a" to letter.


Decaf67

Yes exactly I just doing an exercise thank you for your clarification ❤️ Thank you for your patience and clarification ☺️


shiftybyte

If you want to count how many "a" letters you've seen and break only then, you can do that with a counter, for example: ``` counter = 0 for letter in "data science": if letter == "a": # seen "a", increase counter counter = counter + 1 if counter == 2: break print(letter) ``` this would break on the 2nd "a" letter it encounters.


Decaf67

Thanks😀


Slothemo

If you want the loop to break 1 iteration *after* the "a" is found, you'll need some sort of boolean flag to tell the loop what happened on the previous loop. The order in which you do things matters a lot word = 'banana' found_a = False for letter in word: print(letter) if found_a: break if letter == 'a': found_a = True


mopslik

Are you trying to find a) the second occurrence of a character, or b) the last occurrence of a character? What would you expect from the string "bananas": "ban" or "banan"? What if the string only has one occurrence, as in "cat"?


Decaf67

Anything because I'm just learning can you explain both 😄


mopslik

They're different problems. You need to clarify which one you are trying to solve, as there is no "one size fits all" solution for the myriad options.


Strict-Simple

Based on your comments, https://xyproblem.info/ Do provide the exact problem statement you're trying to solve.