1

in the following example

print ("How old are you?" , input("please input"))

when executing it, why is it that it asks for input propmpt before printing "How old are you?"? What is the order of execution of sections of print statement?

1
  • as a general rule, it is not a good habit to use expressions with side-effect (like input()), or expressions whose values depends on the order of evaluation, inside other expressions (like print). Commented Jun 26, 2013 at 14:37

1 Answer 1

5

Whatever you pass to the print() function has to be executed first. How else would Python know what to pass to the print() function?

Generally speaking, in order for Python to call a function, you need to first determine what values you are passing into that function. See the Calls expression documentation:

All argument expressions are evaluated before the call is attempted.

Calling print() you are passing in a string ("How old are you?"), and the result of calling input("please input"). Python has to execute those sub-expressions first before it can call print().

In this specific case, just use How old are you? as the input() prompt:

age = input("How old are you? ")

and don't bother with print().

If you did want to print How old are you? on a separate line first, call print() with just that string, then on a separate line, call input():

print("How old are you?")
age = input("please input")

Note that input() returns whatever string the user entered, you want to store that somewhere. In my examples, age is that 'somewhere'.

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

3 Comments

but while executing the (print()) function, it passes ("How old are you?") first right? so isnt that supposed to print first?
@user2524557: No, you are passing two arguments to print(). They both need to be evaluated before calling print().
@user2524557 what you are looking for is called Lazy evaluation. very few languages has this feature.

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.