Quick actions

cmd+k|ctrl+k

Navigation

Languages

calculator demo

Snippet info

Language

Python

Visibility

public

Author

miffyying621

Created

2025-03-26T11:39:19.426948Z

Updated

2025-04-09T04:47:51.112056Z

result = None
ls = input(please input a number:")
#ls = '1 + 1 + 1 + 1 + 'in terminal only happens in VS Code
ls = ls.split()
def check_float(data):
    try:
        float (data)
        return True
    except ValueError:
        print ('invalid input')
        return False
while (len(ls) >=3):
    x = ls[0]
    op = ls [1]
    y = ls [2]
    if type(float(x)) == float and type (float(y)) == float:
        x = float(x)
        y = float (y)
        if op in ['+', '-', '*' ,'/']:
            if op == '+':
                result = x + y
            if op == '-':
                result = x - y
            if op == '*': 
                result = x * y
            if op == '/':
                result = x / y
        else:
            print ('wrong operator')
            break
    else:
        print ('wrong operand')
        break
    del ls[:2]
    ls[0] = result
    if len(ls) == 2:
        print ('wrong operands')
        break
       
    if len(ls) == 1:
        break
print(result)
INFO