簡體   English   中英

我該如何編寫這個python輸入代碼? 我所擁有的給了我一條錯誤消息

[英]How can I write this python input code? What I have gives me an error message

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

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

我希望它以我輸入的名字返回“helo person”,但是當輸入並按回車鍵時,我看到的是一條錯誤消息,表明該名稱未定義。

該代碼在 thonny IDE 中運行良好,但在 IDLE shell 中運行良好,任何知道為什么幫助我的人都可以嗎?

我是編程的新手。

如果您收到這樣的錯誤代碼,並且它在您的其他 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)

您正在使用 python 3 樣式的input函數,但您使用的是 python 2.7。 嘗試:

person = raw_input("enter your name:")
print "Halo "+ person

有多種方法可以打印您想要的內容。 嘗試:

print('helo %s' %person)

或者

print('helo {}'.format(person))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM