Skip to content

Instantly share code, notes, and snippets.

@brakkum
Created November 9, 2019 15:18
Show Gist options
  • Save brakkum/bb04c618f61bd705d3c687389fbbaea1 to your computer and use it in GitHub Desktop.
Save brakkum/bb04c618f61bd705d3c687389fbbaea1 to your computer and use it in GitHub Desktop.
Generate a horrible Python calculator
# make a shitty python calculator
with open("./bad_calc.py", "a+") as out_file:
out_file.write("num1 = int(input('enter a number '))\n")
out_file.write("num2 = int(input('enter another number '))\n")
out_file.write("operator = input('what operator? (+, -, /, *) ')\n")
out_file.write("\n")
operators = ["+", "-", "/", "*"]
up_to = 50
for operator in operators:
for num1 in range(up_to + 1):
for num2 in range(up_to + 1):
out_file.write("if num1 == {} and operator == '{}' and num2 == {}:\n".format(num1, operator, num2))
equation = "{}{}{}".format(num1, operator, num2)
if num2 == 0 and operator == "/":
out_file.write(" print(\"sorry, can't divide by zero\")\n")
continue
out_file.write(" print('{} = {}')\n".format(equation, eval(equation)))
out_file.write("\nprint('Goodbye thanks :)')\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment