Python - 根据请求返回语法错误

时间:2017-03-12 23:44:00

标签: python

我正在制作一个小程序,以便创建一个有效的计算器。到目前为止,该程序还不错,但我试图在函数

中设置我的变量时遇到了这个问题
def request(
  val1 = int(input('Enter the first value: '))
  operation1 = (input('''Enter the operation: '''))
  val2 = int(input('Enter the second value: '))
)

由于某种原因,只有第三行(operation1)有错误。不确定python是愚蠢还是我做错了

2 个答案:

答案 0 :(得分:0)

我相信python中的函数语法是:

def fxnName(args_if_any):
    val1 = int(input('Enter the first value: '))
    ...

您正在函数的参数空间中包装所有行。

答案 1 :(得分:0)

Python具有非常严格的函数语法,与其他不需要空格的编程语言不同。此外,您使用的语法不正确,因为您使用“()”来定义函数的外部限制。您的函数的正确版本将是:

注意':'然后是缩进。如果删除缩进,它将无法运行。如果你不使用':'就行不通。

def request():
    val1 = int(input('Enter the first value: '))
    operation1 = (input('''Enter the operation: '''))
    val2 = int(input('Enter the second value: '))
request()