user2524557
user2524557

Reputation: 13

input prompt in a print statement python

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?

Upvotes: 1

Views: 11941

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1123440

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'.

Upvotes: 5

Related Questions