print('What is your name?') # ask for their name
myName = input()
print('It is good to meet you, ' myName)
print('The length of your name is:')
print(len(myName))
當我運行它...
What is your name?
>>> bn
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
bn
NameError: name 'bn' is not defined
版本
>>> import sys; print(sys.version)
3.9.7 (tags/v3.9.7:1016ef3, Aug 30 2021, 20:19:38) [MSC v.1929 64 bit (AMD64)]
uj5u.com熱心網友回復:
>>> bn
這意味著您bn在解釋器 shell 上輸入,而不是之前的輸入函式....
你可能想寫這個而不是先列印
myName = input('What is your name? ')
您的錯誤也可能與 Python2 相關
例子
$ cat /tmp/app.py
print('What is your name?') # ask for their name
myName = input()
print('It is good to meet you, ' myName)
print('The length of your name is:')
print(len(myName))
$ python2 /tmp/app.py
What is your name?
bn
Traceback (most recent call last):
File "/tmp/app.py", line 2, in <module>
myName = input()
File "<string>", line 1, in <module>
NameError: name 'bn' is not defined
$ python3 /tmp/app.py
What is your name?
bn
It is good to meet you, bn
The length of your name is:
2
如果你想使用 Python2,你需要使用raw_input()as對輸入值的input()隱式呼叫eval()
uj5u.com熱心網友回復:
你的環境肯定有問題。您應該考慮卸載 Python 2。但是,也請嘗試運行此代碼并告訴我們輸出是什么:
import sys
print(sys.version[:5])
print('What is your name?') # ask for their name
myName = input()
print('It is good to meet you, ' myName)
print('The length of your name is:')
print(len(myName))
從 Py3.9 在 IDLE 中運行它,我得到以下輸出:
Python 3.9.7 (tags/v3.9.7:1016ef3, Aug 30 2021, 20:19:38) [MSC v.1929
64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or
"license()" for more information.
>>>
==== RESTART: C:\Users\Gary\Google Drive\Projects\StackOverflow\69651615.py ====
3.9.7
What is your name?
bn
It is good to meet you, bn
The length of your name is: 2
>>>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/328529.html
