# 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它会根据用户输入变换相应的类型,而且如果要输入字符和字符串的时候必须要用引号包起来,而raw_input则是不管用户输入什么类型的都会转变成字符型。
解决:
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)