簡體   English   中英

Python初學者試圖了解如何運行input()函數

[英]Python beginner trying to understand how to run input() function

這是我在該網站上的第一篇文章,如果我張貼在錯誤的地方或其他地方,請告訴我。

所以...我使用的是Mac版本的Python 3.x,幾周前我開始學習,在這里遇到一些麻煩。

在文本編輯器中,我編寫並保存了:

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

接着:

> >

但是給我回信:

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

我做錯了什么?

如果您使用的是python 2.7,請在school用雙引號將其作為字符串獲取。

例如Python 2.7空閑的示例:

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

現在還不清楚您做了什么,而且那些“ >>”對我來說有點奇怪,通常python在執行時有3“>”。

python中的輸入函數停止執行並等待,直到用戶鍵入某些內容(或不輸入任何內容)並按回車鍵(輸入)。 您可以像執行操作一樣將用戶從鍵盤輸入的任何內容分配給變量。

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.

需要注意的一件事是:如果以交互方式執行python,它將在要求用戶輸入值后立即要求您輸入輸入。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM