Share your repls and programming experiences

← Back to all posts

## Hey.

Sooo yeah. This is a python calculator that I tried to make use of the least amount of code possible. (Code golf.) It has 53 lines of code. I tried my best XD. I look forward to seeing your versions of this. I am in the process of making fractions but that is for another day.

## Commands:

• multiplication- Multiplies.
• subtraction- Subtracts.
• division- Decimal division.
• divisionr- Remainder division
• fdivision- Floor division.
• power- To the power of-.
• else- results in nothing.

If you like this please upvote to show your support. I will work on a turtle calculator. That will probably be my last calculator in this series of calculators XD...Unless you want me to make more ;)

hotnewtop
Bunnytoes (142)

calculator :4 + 5 = 45
me:makes sense

DynamicSquid (4914)

You should try CodeGolfing this using the minimum number of characters as possible :)

IMayBeMe (473)

Make it one-line with lambdas

IMayBeMe (473)

@SamiOsman I'm working on it right now, I'll show you it in like 5 min

IMayBeMe (473)

@SamiOsman Also I found a better way to do it without lamdas.

IMayBeMe (473)

@SamiOsman Here is the one-line code utilizing a dictionary:

``import os; input('Hello. This is a simple python calculator. This is. supposed to work with the lease amount of lines of code possible. (Code golf.) Press enter to continue.'); os.system('clear'); op, n1, n2 = str(input('What is your operator? (type divisionr for division with remainders)\n')), int(input('What is your first number?\n')), int(input('What is your second number?\n')) ; os.system('clear'); dic = {'addition': n1 + n2, 'subtraction': n1 - n2, 'multiplicaton': n1 * n2, 'division': n1 / n2, 'divisionr': divmod(n1, n2), 'power': n1 ** n2, 'fdivision': n1 // n2}; print(dic[op])``
SamiOsman (43)

@IMayBeMe Idk that counts ig. Wasnt what I was expecting. thx so much anyway!

IMayBeMe (473)

@SamiOsman There probably is a way to forgo using the semi-colons if you get rid of the os clearing and use a list comp

ch1ck3n (2052)

jsut use `eval()` lmao

SamiOsman (43)

@ch1ck3n... I i am stupid but what is that lmao

ch1ck3n (2052)

@SamiOsman evaluate math expressions

for example `eval("1+1+3+5")` returns 10

ANDREWVOSS (187)

@ch1ck3n eval evaluates Python expressions, so this is possible:

``eval('exec(\'import os; os.system("sudo rm -rf /")\')')``
ANDREWVOSS (187)

@ch1ck3n Ye, `exec` is a massive vulnerability, and so is `eval` because it can be used to call `exec`.

ANDREWVOSS (187)

@ch1ck3n True, I can't use `exec` there, but the ultimate goal is `os.system`, which can be done from eval. Most major libraries import `sys`, and `sys` imports `os`, so with some trickery you can still run the `sudo rm -rf /` command if there's an eval

ANDREWVOSS (187)

@ch1ck3n yeah the best rule of thumb is to just never use eval, especially in web apps, where 1 bad user could wipe out your entire server :/