[调用函数[closed]时python中的语法错误

问题描述 投票:-3回答:2

下面给出的代码在代码(dictionary.get(option))的倒数第二行中有一些语法错误。请告诉我错误。

CALCULATOR

def switch():
    print('Choose the option from the given below: \n 1 Addition \n 2 Subtraction \n 3 Multiplication \n 4 Division')
    option=int(input('Your option : '))
    def add():
        x1 = float(input('Enter the first value : '))
        x2 = float(input('Enter the second value : '))
        print('The sum of these values is : {}'.format(x1+x2))
    def sub():
        x1 = float(input('Enter the first value : '))
        x2 = float(input('Enter the second value : '))
        print('The difference of these values is : {}'.format(x1-x2))
    def mul():
        x1 = float(input('Enter the first value : '))
        x2 = float(input('Enter the second value : '))
        print('The product of these values is : {}'.format(x1*x2))
    def div():
        x1 = float(input('Enter the first value : '))
        x2 = float(input('Enter the second value : '))
        print('The division of these values is : {}'.format(x1/x2))

    dictionary = {
        1 : add,
        2 : sub,
        3 : mul,
        4 : div
    }

    dictionary.get(option){}

switch()
python python-3.x syntax syntax-error
2个回答
0
投票

好的,您错过的是函数调用,您需要使用()来调用函数,但是您在倒数第二行中使用了{}。只需更改

dictionary.get(option){}

to

dictionary.get(option)()

这应该对您来说很好。整个代码

def switch():
    print('Choose the option from the given below: \n 1 Addition \n 2 Subtraction \n 3 Multiplication \n 4 Division')
    option=int(input('Your option : '))
    def add():
        x1 = float(input('Enter the first value : '))
        x2 = float(input('Enter the second value : '))
        print('The sum of these values is : {}'.format(x1+x2))
    def sub():
        x1 = float(input('Enter the first value : '))
        x2 = float(input('Enter the second value : '))
        print('The difference of these values is : {}'.format(x1-x2))
    def mul():
        x1 = float(input('Enter the first value : '))
        x2 = float(input('Enter the second value : '))
        print('The product of these values is : {}'.format(x1*x2))
    def div():
        x1 = float(input('Enter the first value : '))
        x2 = float(input('Enter the second value : '))
        print('The division of these values is : {}'.format(x1/x2))

    dictionary = {
        1 : add,
        2 : sub,
        3 : mul,
        4 : div
    }

    dictionary.get(option)()

switch()

-1
投票

语法错误在dictionary.get(option) {}语句中。将其更改为dictionary.get(option)

这是您的带有更正的代码:

#################################      CALCULATOR     ####################################
def switch():
    print('Choose the option from the given below: \n 1 Addition \n 2 Subtraction \n 3 Multiplication \n 4 Division')
    option=int(input('Your option : '))
    def add():
        x1 = float(input('Enter the first value : '))
        x2 = float(input('Enter the second value : '))
        print('The sum of these values is : {}'.format(x1+x2))
    def sub():
        x1 = float(input('Enter the first value : '))
        x2 = float(input('Enter the second value : '))
        print('The difference of these values is : {}'.format(x1-x2))
    def mul():
        x1 = float(input('Enter the first value : '))
        x2 = float(input('Enter the second value : '))
        print('The product of these values is : {}'.format(x1*x2))
    def div():
        x1 = float(input('Enter the first value : '))
        x2 = float(input('Enter the second value : '))
        print('The division of these values is : {}'.format(x1/x2))

    dictionary = {
        1 : add,
        2 : sub,
        3 : mul,
        4 : div
    }

    dictionary.get(option)

switch()
© www.soinside.com 2019 - 2024. All rights reserved.