Share your repls and programming experiences

← Back to all posts
Inline Calculator: A Very Simple One Line Calculator (Not Using eval())
BuddyRaider (24)
hello world

Inline Calculator!


Python, gotta love it, a language focused so firmly on readability and cleanliness...

And then, we have people like me, @rafrafraf (sorry for the ping), and many more, who forget all the supposed rules and make the craziest things in only one line!


So, what did I do this time?


I made a calculator!

But not just any calculator, and not just one of those fake ones using eval().

Rather, one that splits the input into two numbers, and performs an operation on them based off the text that was in between!


That's IT???

No!


THIS calculator is modular, you can add in the code as many operations as your heart desires!
(Just to clarify, cuz a few people got confused by this, I can add in the code as many operations as I want, I am currently still trying to add in a feature to do multiple operations at once)


Why are you telling us this?


Because I want you all to recommend any new operations to add to my calculator!

I want you to challenge me to push my limits with this calculator!

As of posting this, there are only four operations (+, -, *, /), so I will post any changes/recommendations below!


Changes



P.S. Check out my other post on an Inline Matrix-style Binary program (MATRIX)
andpleasedonotkillmeindacomments rafrafraf lol
Comments
hotnewtop
FlaminHotValdez (430)

I challenge you to make it so that it has capacity beyond [number][number]. As far as I know there are only 3 calculators on repl.it that have capacity beyond [number][number] without using eval() or similar functions.

Whippingdot (547)

Oh I am still making my cal. I am rn stuck on the 10s and above and I am stuck on the more than one calculation (like this dude). @FlaminHotValdez

FlaminHotValdez (430)

@Whippingdot Stack for multiple operators, string parsing for double digit/decimal.

Whippingdot (547)

I am using an indexing method to get the numbers and am using a for loop to get the digits added and all. some of the indexes are going wrong so... I am trying to fix them. @FlaminHotValdez

FlaminHotValdez (430)

@Whippingdot so you go over each index. If the index is a number say it's a number, if the next one is also a number multiply the number by 10 then add the next digit, etc

Whippingdot (547)

Yea but the index gets mixed up when there is a number more than the units place as you have to find out the length of the numbers so you can add the index relatively. You can help me on this calc if you want, do you want to? I will invite you to a new repl if you want. Currently i am using VS Code cause this was just supposed to be an easy project to re-initiate my python skills. @FlaminHotValdez

FlaminHotValdez (430)

@Whippingdot Ah okay, can you share a repl with me?

why you using vscode AND repl?

Whippingdot (547)

https://amasad.me/replit_ide dat is why. And I will share it rn. Sorry for the late reply...I saw the email but was too lazy to reply. @FlaminHotValdez

BuddyRaider (24)

Yeah, I knew someone would get at me for that, but for one of my first inline projects, you have to admit it isn't too bad, and at least I am not using eval() lol @FlaminHotValdez @Whippingdot

FlaminHotValdez (430)

@BuddyRaider Yeah I'll give you that a oneline calculator is difficult to make.

BuddyRaider (24)

lol look at my reply to TalinSharma @FlaminHotValdez

btw leo valdez ftw
cjmatthy09 (14)

hey! i've got a challenge for ya. Gauss-Legendre pi calculating algorithm in one line.
ask how many iterations the user wants, and do it! here's the source for my multiline method: https://github.com/MatthyPlayz/pickle/blob/main/pickl/methods.py , line 49

BuddyRaider (24)

@cjmatthy09 Thanks! I gotta try that out, it sounds fun :D

you get a cycle :D
edit: I will reply your comment when I finish and share it on repl.it/talk
cjmatthy09 (14)

@BuddyRaider didn't expect you to try it but dang gl

you get a cycle too :D
BuddyRaider (24)

Working on it right now lol @cjmatthy09

It is tough tho tbh XD
Romulus777 (2)

Very cool, maybe ina few years you might have sines, cosines, and tangents

BuddyRaider (24)

@Romulus777 It shouldn't take years lol

Hopefully only a few minutes with the Python math module XD

still get your (first) cycle tho lol
JacobMcPherson1 (189)

Does it only have one line?! That's such low effort dude

BuddyRaider (24)

@JacobMcPherson1 lol

Not when it is for a fully functional calculator XD

ch1ck3n (1557)

@JacobMcPherson1 Raf posted 4 one liners lol

JacobMcPherson1 (189)

@ch1ck3n I am aware of that Mr.Chicken

BuddyRaider (24)

I mentioned raf in my post because he had the most popular one liners @ch1cken

Whippingdot (547)

bRo STOP COPYING MY COMMENT FROM THE OTHER GAME YOU PERSON GREEDY FOR UPVOTES. NO-ONE UPVOTE THIS THIS IS LIKE THE EXACT SAME COMMENT I MADE IN RAFRAFS ONE LINER. @JacobMcPherson1

BuddyRaider (24)

chill dude, why do you have to go and bite his head off just cuz he wanted to say a joke? @Whippingdot

just cuz you aren't happy/want to accuse me of lying
JacobMcPherson1 (189)

@Whippingdot I didn't know you made the same joke as me, I'm not copying you on purpose

Whippingdot (547)

ya right I made the same joke on a one liner. Also that wasn't meant to be serious (i was just tOo lAzY tO dO tHiS tHe wHoLe tImE) @JacobMcPherson1

Whippingdot (547)

When did I say u were lying? I was talking to the other dude. @BuddyRaider

Whippingdot (547)

No it was me...on another comment tho...so why did you reply here? @BuddyRaider

BuddyRaider (24)

I replied here originally because I saw a ton of all caps lol @Whippingdot

I am all confused with you and Valdez commenting everywhere lol
Is this what it is like on one of your popular posts with hundreds of comments lol
Whippingdot (547)

also you need to truncate the answer if it isn't a decimal @BuddyRaider

BuddyRaider (24)

Alright, @Whippingdot, I truncated the answer if it isn't a decimal, and I would have done it sooner, except I was trying to add multiple operations (It's hard without hard coding every possibility in XD) lol

Whippingdot (547)


This trash please don't lie. You can only do two operations and after that it doesn't work. Stop lying.

BuddyRaider (24)

@Whippingdot I am sorry, but I also do not remember saying it can do more than two operations lol

Whippingdot (547)

I thought that was what this meant...

@BuddyRaider

BuddyRaider (24)

Yeah I meant that in the code I can add as many operations :P oopsie, my bad lol @Whippingdot

FlaminHotValdez (430)

This calculator only has capacity for two numbers and one operator in between...and it says you can have as many operations as your heart desires.

Yet:

1+2+3=3
1+2x2 = Traceback (most recent call last): File "main.py", line 1, in <module> while True: print((lambda x=input("Please Enter Equation: "), sin=__import__("math").sin, cos=__import__("math").cos, tan=__import__("math").tan, deg=__import__("math").radians: float(x.split("+")[0]) + float(x.split("+")[1]) if "+" in x else float(x.split("-")[0]) - float(x.split("-")[1]) if "-" in x else float(x.split("*")[0]) * float(x.split("*")[1]) if "*" in x else float(x.split("/")[0]) / float(x.split("/")[1]) if "/" in x else float("".join(x.split("^"))) ** float(x.split("^")[1]) if "^" in x else sin(deg(float("".join(x.split("sin"))))) if "sin" in x else cos(deg(float("".join(x.split("cos"))))) if "cos" in x else tan(deg(float(x.split("tan")[0]))) if "tan" in x else "Operation Error")()) File "main.py", line 1, in <lambda> while True: print((lambda x=input("Please Enter Equation: "), sin=__import__("math").sin, cos=__import__("math").cos, tan=__import__("math").tan, deg=__import__("math").radians: float(x.split("+")[0]) + float(x.split("+")[1]) if "+" in x else float(x.split("-")[0]) - float(x.split("-")[1]) if "-" in x else float(x.split("*")[0]) * float(x.split("*")[1]) if "*" in x else float(x.split("/")[0]) / float(x.split("/")[1]) if "/" in x else float("".join(x.split("^"))) ** float(x.split("^")[1]) if "^" in x else sin(deg(float("".join(x.split("sin"))))) if "sin" in x else cos(deg(float("".join(x.split("cos"))))) if "cos" in x else tan(deg(float(x.split("tan")[0]))) if "tan" in x else "Operation Error")()) ValueError: could not convert string to float: '2*2'

BuddyRaider (24)

@FlaminHotValdez Sorry, I must have explained it wrong or something, I meant I can add in as many as I want, not that the user can use as many as they want lol

sorry 'bout that
BuddyRaider (24)

Do not forget to leave suggestions! :D

I will updoot any comment with a suggestion that I end up using! :O
BuddyRaider (24)

Thanks @ch1ck3n I am really glad you like it :D

ch1ck3n (1557)

@BuddyRaider you should add more exponents stuff

BuddyRaider (24)

I wanted to see how long it would take for someone to reccomend that lol XD @ch1ck3n

you get a (free?) updoot...yay!
BuddyRaider (24)

Exponents added with ^ operator @ch1ck3n

BuddyRaider (24)

Approval from the inline master himself @rafrafraf

...

My life is complete...
btw thx for updoot :P
Whippingdot (547)

Bruh he lied more than one calculation doesn't work. 1+1+1+1+1+1+1+1+1 = 2 says the calculator. @rafrafraf

BuddyRaider (24)

@Whippingdot I did not lie, either I did not explain it clearly enough, or you misunderstood it