简体   繁体   中英

Python beginner trying to understand how to run input() function

this is my first post on this site and please tell me if I posted on wrong place or something.

So... I'm using Mac version of Python 3.x which I started learning a few weeks ago and am facing a bit of trouble understanding here.

In the text editor, I wrote and saved:

>a = input("> ") <br>
print("A boy goes to" + a)

And then:

> >

But returned me with:

> > school
Traceback (most recent call last):
  File "workspace/main.py", line 3, in <module>
    a = input("> ")
  File "<string>", line 1, in <module>
NameError: name 'school' is not defined

What did I do wrong?

If you are using python 2.7 write school in double quotes to get it as string.

Eg an example from Python 2.7 idle:

>>> a = input("> ")
> "school"
>>> print("A boy goes to " + a)
A boy goes to school

It's a bit unclear what you've done, and those "> >" are a bit odd to me, usually python has 3 ">" when you execute it.

The input function in python stops the execution and waits until the user types something (or nothing) and presses the return key (enter). You can assign whatever the user inputed from his keyboard into a variable, as you did.

variable = input("Some text to show the user what he should do")
# Execution will stop until user presses enter
print(variable)  # Will print whatever the user typed when the above text was printed to him.

One thing to notice is: if you execute python in interactive mode, it will ask for you to enter your input right after you ask for the user's input value.

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