简体   繁体   中英

What is the purpose of the parentheses after the input statement?

What is the purpose of the parentheses after the input statement?

>>> name = input()
>>> print(name)

input is a function. To call a function you need brackets.

See this example:

# Defining a function to print 'Hi'
def print_hi():
    print('Hi')

print_hi # Does nothing because you haven't called the function
print_hi() # Prints 'Hi' because the function has been called

This is the same with input , and all other functions

@Ahad Mosharraf @MisterMiyagi and others. Thank you all for your answers. It will really help me. I have also done some in-depth research and written an explanation. I am waiting for your opinions.

The input() function allows your programs to accept user input or your own input.

Here comes an example:

age = input("What is your age? ")

Let's have a look at this function step by step.

  1. "input" is the part of the function that asks for your "age" (What is your age?)

  2. The parentheses (...) mean that this function takes an argument.

  3. "What is your age?" --> "What is your age is your argument."

  4. That prompt will be displayed to the user or to ourselves

warning: don't forget to enter your prompt. If you leave this blank you may get unexpected results.

  1. age = now the returned value is held by the "age" variable (the chosen "age")

I hope this explanation is good and clear enough!

in Python the parentheses after input function is used to show a message to users. For an example if you want to show "Input Your Name: " you need to write it in the parentheses like name=input("Input Your Name: ") :-)

input is a function . Functions are used to run a certain block of code. I suppose you know how variables work, if you just typed input Python would think you're trying to access the variable called function . But if you add brackets, Python knows that you are trying to call input .

So actually what happens if you type input() is:

-Python gets the function called input .

-Calls that function/executes the code inside that function/accepts user input from the terminal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM