-1

我在 python 文件中有以下代码first.py

person=input("enter your name:")
print("helo ",person)

我希望它返回带有我输入的名称的“helo person”,但是当输入并按 Enter 时,我看到的只是一条错误消息,表明该名称未定义。

该代码在 thonny IDE 中运行良好,但在 IDLE shell 中运行良好,任何知道为什么的人都可以帮助我吗?

我是编程新手。

4

3 回答 3

1

如果您收到这样的错误代码,并且它在您的其他 IDE 中按预期工作,那么 IDLE 似乎使用的是 python 2.x,输入/打印语法的效果略有不同。

蟒蛇 2

person = raw_input("enter your name: ")
print("hello {}".format(person))

蟒蛇 3

person = input("enter your name: ")
print("hello ",person)
于 2018-06-15T09:49:11.553 回答
0

您正在使用input具有 python 3 样式的函数,但您使用的是 python 2.7。尝试:

person = raw_input("enter your name:")
print "Halo "+ person
于 2018-06-15T09:55:56.580 回答
-1

有多种方法可以打印您想要的内容。尝试:

print('helo %s' %person)

或者

print('helo {}'.format(person))
于 2018-06-15T09:42:24.043 回答