Home > Mobile >  Building a calculator with a list input with Python
Building a calculator with a list input with Python

Time:10-05

Would someone be able to tell me why this code doesn't work?

Task specs:

*Our function should take a single list of strings as an argument, containing our first operand, followed by an operator and then the 2nd operand. eg: simple_calculator(['1', ' ', '1' ]) #=> 2 The function should return 'Please enter valid format: [Operand, Operator, Operand]' for arguments that don’t match the required format The function should work with the following simple operators , -, *, /, %, and should return'Please enter a valid operator [ , -, /, , % ]' if any other operators are passed Assume that the operands are always numeric (floats or integers); no need to validate their data type

MY CODE:

def simple_calculator(input):
    if type(input[1]) == int and type(input[2])==int
        return
        if input[1] == ' ':
            print(add(input[0],input[2]))

        if input[1] == '-':
            print(subtract(input[0],input[2]))

        if input[1] == '/':
            print(divide(input[0],input[2]))

        if input[1] == '*':
            print(multiply(input[0],input[2]))
        else:
            return "Please enter a valid operator [  , -, /, *, % ]"
    else: 
        return "Please enter valid format: [Operand, Operator, Operand]"

CodePudding user response:

Firstly you need a colon at the end of this line

if type(input[1]) == int and type(input[2])==int

then you need to remove the return statement immediately below that because this will cause your function to return immediately so nothing below this line will be run.

input is also a reserved word in python, so I'd choose a different variable name.

If your input is ['1', ' ', '1' ] then this will also fail because '1' is a string not an int. Input should be [1, ' ', 1 ] or you should convert the strings to ints.

I'd fix these issues and try again.

CodePudding user response:

You should add colon at the end of the if statement

if type(input[1]) == int and type(input[2])==int

then try removing the return statement as anything in the code block below it will not run (it returns immediately).

This however does not check for your input correctly as you will receive string, so you should convert it to the int yourself. See this.

Also, try not to use input as a variable name as it is keyword in python.

After correcting these, take a look at the if statements. You should use elif as you want to check if the operator is one of [ , -, /, *, % ], but as of now you will raise the operator error even if it is correct. For example using will raise this error even when it should not.

Final code should be looking something like this:

def simple_calculator(inp):
    try:
        int(inp[0])
        int(inp[2])
    except ValueError:
        return "Please enter valid format: [Operand, Operator, Operand]"
    if inp[1] == ' ':
        print(add(inp[0],inp[2]))
    elif inp[1] == '-':
        print(subtract(inp[0],inp[2]))
    elif inp[1] == '/':
        print(divide(inp[0],inp[2]))
    elif inp[1] == '*':
        print(multiply(inp[0],inp[2]))
    else:
        return "Please enter a valid operator [  , -, /, *, % ]"

Also make sure to add check for % operator.

  • Related