简体   繁体   English

Python初学者试图了解如何运行input()函数

[英]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. 所以...我使用的是Mac版本的Python 3.x,几周前我开始学习,在这里遇到一些麻烦。

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. 如果您使用的是python 2.7,请在school用双引号将其作为字符串获取。

Eg an example from Python 2.7 idle: 例如Python 2.7空闲的示例:

>>> 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. 现在还不清楚您做了什么,而且那些“ >>”对我来说有点奇怪,通常python在执行时有3“>”。

The input function in python stops the execution and waits until the user types something (or nothing) and presses the return key (enter). python中的输入函数停止执行并等待,直到用户键入某些内容(或不输入任何内容)并按回车键(输入)。 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. 需要注意的一件事是:如果以交互方式执行python,它将在要求用户输入值后立即要求您输入输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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