语法无效,我找不到问题

时间:2016-12-20 23:03:37

标签: python python-3.x

对不起,编程很新手,但我似乎无法在这里找到问题。 Python说问题是在最后一个括号和双引号之间。

b = input("Tell me,", name", what can you see in front of you? ")

如果我将双引号更改为单引号或更改文本,则问题仍然存在。

3 个答案:

答案 0 :(得分:2)

您需要在name之后和"之前使用逗号,以便将变量与其后的字符串分开。

b = input("Tell me,", name", what can you see in front of you? ")   # ERROR
b = input("Tell me,", name, ", what can you see in front of you? ") # NO SYNTAX ERROR

但是,请注意input()只接受一个参数而不是多个参数,所以为了做到这一点,你实际上想要使用+来连接字符串而不是传递多个参数。尝试将多个参数传递给input()会给你一个TypeError

正如其他多个答案所指出的那样,对input()使用字符串连接可能会有点混乱,尤其是当您尝试基本连接字符串中所有一个句子的部分时。事先使用字符串格式化操作符可以获得更清晰的代码:

b = input("Tell me, {}, what can you see in front of you? ".format(name))

您传递给format的字符串将插入{}占位符所在的格式字符串中。

答案 1 :(得分:0)

", name"

周围有逗号丢失

但是这无论如何都不会起作用,因为input()不像print()那样有效。

In [5]: b = input("Tell me,", name, ", what can you see in front of you? ")
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-3b47224115df> in <module>()
----> 1 b = input("Tell me,", name, ", what can you see in front of you? ")

TypeError: input expected at most 1 arguments, got 3

您可以使用字符串格式。会更清楚地看到。

b = input("Tell me, {} what can you see in front of you?".format(name))

答案 2 :(得分:0)

字符串格式将使这更具可读性并且不易出错:

b = input("Tell me, {0}, what can you see in front of you? ".format(name))