Quick actions

cmd+k|ctrl+k

Navigation

Languages

Calculator

Snippet info

Language

Python

Visibility

public

Author

shirley

Created

2025-10-22T11:52:37.495667Z

Updated

2025-10-22T12:45:50.359646Z

statement = ['1', '/', ]
while (len(statement) >= 3 and len(statement) % 2):
    x = statement[0]
    y = statement[2]
    op = statement[1]
    try:
        x = float(x)
        y = float(y)
        # print (x, op, y, 'is equal to:')
    except ValueError:
        print('ValueError')
        break
    if (op in ['+', '-', '*', '/']):
        result = 0
        if op == '+':
            result = x + y
        if op == '-':
            result = x - y
        if op == '*':
            result = x * y
        if op == '/':
            if y == 0:
                print('y cannot be zero!')
                break
            else: 
                result = x / y
        del statement [:2]
        statement [0] = result
        if len(statement) == 1 :
            print(result)
    else:
        print('Wrong operator !')
        break
else:
    if (len(statement) % 2 == 0) :
        print('Incomplete')
INFO