英文:
Why doesn't it print "hello" first?
问题
在这段代码中:
print("Hello " + input("What is Your name?") + "!")
当我运行它时,它会打印出 "What is your name?" 为什么它不先打印出 "hello" 呢?它应该先询问用户输入他的名字。
英文:
In this code:
print("Hello " + input("What is Your name?")+ "!")
When I run it, it prints "What is your name?" Why doesn't it print "hello" first? It should ask the user for input for his name.
答案1
得分: 4
在数学中,与内括号中的内容相似,必须首先进行评估。因此,Python执行以下操作:
input("你叫什么名字?")
然后对参数列表中生成的字符串进行连接,并执行print()
。
英文:
Like in mathematics, anything in inner parentheses has to be evaluated first. So Python does
input("What is your name?")
and then performs print()
on the concatenation of the resulting strings in the argument list.
答案2
得分: 2
input()
等待用户输入。所以,如果你输入一个名字,它会读取输入然后继续执行剩余的代码(字符串连接和打印):
>>> print("Hello " + input("你的名字是什么?\n") + "!")
你的名字是什么?
Foo # 由你输入!
Hello Foo!
英文:
input()
waits for -- well -- user input.
So if you enter a name, it will read said input and continue executing the rest of the code (string concatenation and print):
>>> print("Hello " + input("What is Your name?\n")+ "!")
What is Your name?
Foo # typed by you!
Hello Foo!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论