为什么它不首先打印 “hello”?

huangapple go评论72阅读模式
英文:

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!

huangapple
  • 本文由 发表于 2023年5月17日 19:57:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76271834.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定