1

I'm looking for the syntax I would use to print both text and a saved int in the input function. I'm making a program that asks the user math questions using dynamic terms, and I want to be able to print those and some text on the same line.

I've tried this code, hoping that the parameters would be represented like it were a print statement:

answer = int(input(oneTerm, '+', twoTerm, '='))

I also tried using two lines like this:

print(oneTerm, '+', twoTerm, '=')
answer = int(input(''))

But the problem with this is the input is accepted on the line below the print statement, but I need it to be on the same line.

Any help is greatly appreciated!

3 Answers 3

2

The input() function takes only one string argument (and not multiple, like print()). Use string formatting:

answer = int(input('{} + {} = '.format(oneTerm, twoTerm)))

The {} placeholders are replaced by the two arguments to .format(), creating essentially the same string to prompt the user for input.

See the str.format() method documentation for more details.

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

Comments

1

This is a very old question but I guess the best solution would be using F-strings for an updated aproach. This was released in Python >=3.6 in December 2016 (see PEP 498).

In a single line it would be:

answer = int(input(F'{oneTerm} + {twoTerm} ='))

I guess this is the more updated way to do it and also the most readable. What you guys think about readability with F-strings? It's very nice, right?

Comments

-1

Instead of these methods simply try using:

answer = input(oneTerm, '+', twoTerm, '=')
answer = int(answer)

1 Comment

input expected at most 1 argument

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.