How to break a loop in print function that has "end = "

for values in ('32' , '23'):
         print(values, end = ' + ')
for values in (56' , '92'):
          print(values, end = ' + ')

Output I want :** 32 + 23
****************** 56 + 92
Output I’m getting: 32 + 23 + 56 + 92 +

Can anyone help me fix this?? How can i break out of the loop with "end = "

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Thanks for that…
I’ll implement it next time
So, can you help with my question pls??

Sorry, I don’t do Python. I just wanted to clean up your formatting, especially since it is so important in Python.

Ohh…
Thanks nonetheless
Just being curious, what language do you work with?

I’m a React Native developer. So, JavaScript (or TypeScript) in React Native.

I mean, you can’t? You are telling Python to make a loop and execute that print-command.

If you want Python to do something different in the last loop, you need either shorten the loop and have the last entry executed after the loop OR design the loop with a counter, get the actual length of the loop and an if-statement to execute different code in the final loop.

myval = 1000;
    for i in range(myval):
        if(i == myval -1):
            #something

Messing around with a Python emulator, after fixing some typos…

The end keyword replaces the NL at the end of the line, without that, the line won’t print. Maybe there is a more elegant way, but if I stick print('') at the end of your program, it tags on a NL so it finally prints that line.

I’m actually asking this question to solve the arithmetic calculator project
My code looks like this

numerator = ['  345' ,'  635', '  523']
denominator = ['+ 656', '+ 241', '+ 161']

when i try to do this:

print(''.join(numerator))
print(''.join(denominator))

It just prints out the problems accordingly without space in between
But what i actually want is 4 spaces between each problem according to the task…
So, i’m trying to loop through the ‘numerator’ and ‘denominator’ variable and add 4 spaces(i.e ’ ')between them and then print everything out
I want my numerator and denominator to finally look like this

numerator = ['problem', '      ', 'problem', '     ', 'problem']
denominator = ['problem', '      ','problem','      ', 'problem']

I dont really know if i’m explaning well enough

In this case, you are not supposed to print values, but to create a string and then return that.
While you still cannot easily get the last loop, you can just slice off the end of the string or use some strip-method if it’s only spaces.

I tried doing that but it gave me an extra new line…
I dont know why though

Good enough - I mean, I know the project :wink:
And I gave you a couple of options of how to achieve this.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.