# This program says hello and asks for my name.
print('hello world!')
print('What is your name?') # ask for their name
myName = input()
print('It is good to meet you,' + myName)
運行時報錯:
hello world!
What is your name?
lee
Traceback (most recent call last):
File "p1.py", line 5, in <module>
myName = input()
File "<string>", line 1, in <module>
NameError: name 'lee' is not defined
解釋:
input它會根據(jù)用戶輸入變換相應的類型,而且如果要輸入字符和字符串的時候必須要用引號包起來,而raw_input則是不管用戶輸入什么類型的都會轉(zhuǎn)變成字符型。
解決:
1.輸入人名時,手動加入引號
hello world!
What is your name?
輸入 'lee'
2.將Python文件的input改為raw_input
print('hello world!')
print('What is your name?') # ask for their name
myName = raw_input()
print('It is good to meet you,' + myName)