0

I have a python program which works as a calculator.

import re


value = 0
run = True

while run:
    if value == 0:
        equation = input('Enter the equation ')
    else:
        equation=input(str(value))
        # print(equation + ' ')
    if equation == 'quit':
            run = False
    else:
        equation = re.sub("[a-zA-Z""]",'',equation)
        if value ==0:
            # equation=eval(equation)
            value=eval(equation)
        else:
            value=eval(str(value)+equation)
            print(equation + ' ')

The program works without problem , but when reading the input after the first iteration , the terminal is as below

python3 file.py
Enter the equation 10+10
20+30

The next input can be provided next to 20. I want to add a space before reading the input , so the output will be as shown below

python3 file.py
Enter the equation 10+10
20 +30

How can I add a space before reading the next input so the cursor will create a space

Example

input=input('Enter the input ')

Python Version : 3.7.7

2 Answers 2

2

You may simply append a space to the string given to input.

In your example, changing the line equation=input(str(value)) to equation=input(str(value) + ' ') will produce your desired output.

Sign up to request clarification or add additional context in comments.

Comments

2

IMHO, this line is not what you want:

equation=input(str(value))

Outputting the result of the calculation should be separate from the next input because of semantics. The argument to input() is the prompt to tell the user what to do. "20" (or whatever intermediate result) is not a good instruction for the user.

So the code becomes

print(value, end=" ")    # end=" " prevents a newline and adds a space instead
equation=input()         # don't give any prompt, if there's no need to

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.