尝试在Python中定义常规运算符时对语法的回溯

时间:2019-02-12 22:07:43

标签: python

我的代码的目标是定义一个函数,该函数将允许我输入+-*或/然后输入2个值,并让该函数评估提供的数字

def basic_op(operator, value1, value2):
    try:    
        operator = input('Choose operator: +, -, * or /')
        value1 = int(input('Value1:'))
        value2 = int(input('Value2:'))
            if operator == '+'
                return(value1 + value2)
            elif operator == '-'
                return(value1 - value2)
            elif operator == '*'
                return(value1 * value2)
            else: operator == '/'
                return(int(value1 / value2))
     except:
         print("Invalid value entered")

if / elif / else行的末尾出现语法错误,我不确定自己做错了什么

1 个答案:

答案 0 :(得分:0)

正确的缩进在python中非常重要。 所以您应该这样做。(不要缩进if语句)

def basic_op(operator, value1, value2):
    try:    
        operator = input('Choose operator: +, -, * or /')
        value1 = int(input('Value1:'))
        value2 = int(input('Value2:'))
        if operator == '+'
            return(value1 + value2)
        elif operator == '-'
            return(value1 - value2)
        elif operator == '*'
            return(value1 * value2)
        else: operator == '/'
            return(int(value1 / value2))
     except:
         print("Invalid value entered")