Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 16981

Calculator in one (terminal) line [duplicate]

$
0
0

Understand that when I say "one line" I don't mean one line of code but instead one line of inputso instead of

5*840

It would just be

5\*8 \40

or even

5\*8=40

and could be multiple digits like so:

54+66=120

I don't want the exact answer, this is a small project where I'm messing around with Python to get better and just knowing exactly what needs done would ruin the point. Just a general idea of what to look in to would be great.

Also, So stack overflow doesn't say this is a duplicate question:
I am asking how to take a string and take certain pieces to use for if statements
5*8
and it registers as:
number operator number

Here's my code

num1 = int(input("First Number:"))symOper = input("Operation Symbol:")num2 = int(input("Second Number:"))if symOper == "*":    solve = num1 * num2    print(solve)if symOper == "/":    solve = num1 / num2    print(solve)if symOper == "+":    solve = num1 + num2    print(solve)if symOper == "-":    solve = num1 - num2    print(solve)

EDIT

I found an answer that was simple and also reusable so I'm sharing it here.User @AndreiS typed

# extract numbers from garbage string:s = '12//n,_@#$%3.14kjlw0xdadfackvj1.6e-19&*ghn334'newstr = ''.join((ch if ch in '0123456789.-e' else '') for ch in s)listOfNumbers = [float(i) for i in newstr.split()]print(listOfNumbers)[12.0, 3.14, 0.0, 1.6e-19, 334.0]

Viewing all articles
Browse latest Browse all 16981

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>