Share your repls and programming experiences

← Back to all posts
Python claculator- probably my second to last one.
SamiOsman

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.
  • addition- Adds.
  • 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 ;)

Voters
SamiOsman
Comments
hotnewtop
Bunnytoes

calculator :4 + 5 = 45
me:makes sense

DynamicSquid

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

IMayBeMe

Make it one-line with lambdas

SamiOsman

@IMayBeMe An example?

IMayBeMe

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

IMayBeMe

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

IMayBeMe

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

SamiOsman

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

IMayBeMe

@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

jsut use eval() lmao

SamiOsman

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

ch1ck3n

@SamiOsman evaluate math expressions

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

ANDREWVOSS

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

ch1ck3n
ANDREWVOSS

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

ch1ck3n
SamiOsman

@ANDREWVOSS @ch1ck3n Lemme see what I can do...

ANDREWVOSS

@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

ch1ck3n
ANDREWVOSS

@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 :/