0

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]"
Zuzanna
  • 11
  • 2
  • Please do not use reserved words like **input** for your variables. **input** is a special function in python. Please choose any other name, e.g. **user_input** – artemonsh Oct 05 '22 at 10:29

2 Answers2

0

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.

Vorkos
  • 212
  • 1
  • 11
0

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 [ +, -, /, *, % ]"

This solution will work if you write functions for operators taking in strings such as:

def add(num1, num2):
    return int(num1) + int(num2)

Also make sure to add check for % operator and think about returning instead of printing.

Martin B
  • 11
  • 3