我在 python 文件中有以下代码first.py
person=input("enter your name:")
print("helo ",person)
我希望它返回带有我输入的名称的“helo person”,但是当输入并按 Enter 时,我看到的只是一条错误消息,表明该名称未定义。
该代码在 thonny IDE 中运行良好,但在 IDLE shell 中运行良好,任何知道为什么的人都可以帮助我吗?
我是编程新手。
我在 python 文件中有以下代码first.py
person=input("enter your name:")
print("helo ",person)
我希望它返回带有我输入的名称的“helo person”,但是当输入并按 Enter 时,我看到的只是一条错误消息,表明该名称未定义。
该代码在 thonny IDE 中运行良好,但在 IDLE shell 中运行良好,任何知道为什么的人都可以帮助我吗?
我是编程新手。
如果您收到这样的错误代码,并且它在您的其他 IDE 中按预期工作,那么 IDLE 似乎使用的是 python 2.x,输入/打印语法的效果略有不同。
person = raw_input("enter your name: ")
print("hello {}".format(person))
person = input("enter your name: ")
print("hello ",person)
您正在使用input具有 python 3 样式的函数,但您使用的是 python 2.7。尝试:
person = raw_input("enter your name:")
print "Halo "+ person
有多种方法可以打印您想要的内容。尝试:
print('helo %s' %person)
或者
print('helo {}'.format(person))