#๐Ÿ”’ Simple(?) Calculator

53 messages ยท Page 1 of 1 (latest)

keen trail
#

making a calculator ive got + , - , * and / down
but those need 2 inputs
and ive got 4 more options that would only need 1 input and im not sure how to go about it

# Calculator e'-'e

def calculator():
    
    while True:
        print("Please select what calculation you'd like to perform:\n")
        menu = int(input('''
                1.) Add
                2.) Subtract
                3.) Multiply
                4.) Divide
                5.) Square
                6.) Cube
                7.) Square Root
                8.) Cube Root
                9.) Exit'''))
        
        try: 
            x = float(input("Please enter your first number:\n"))
            y = float(input("Please enter your second number:\n"))
        except ValueError as ERROR:
            print("Input given was not a number.")
            print(ERROR)
            return
        
        if menu == 1:
            answer = x + y
            
        elif menu == 2:
            answer = x - y
        
        elif menu == 3:
            answer = x * y
            
        elif menu == 4:
            answer = x / y
alpine troutBOT
#

@keen trail

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

keen trail
#

ill add the exception handling for possible ZeroDivisionError down the line
just
stuck on how to change it so that it would only need 1 input as opposed to 2

keen trail
#

if the user wants to square 1 number
or cube it
square / cube root it

i see no use in needing 2 numbers to square it e'-e'

weak cloud
#

ah that's what you meant

keen trail
#

indeed

weak cloud
#

well same way as how you're trying to evaluate user calculations

#

use if

#

if menu is square, ask for 1 input.

keen trail
#

would i need to change up my inputs or possibly just move them into my if statements so

if menu is add
it would ask for input x / y
so on so forth

weak cloud
#

sure you could do that

#

or

#

turn them into functions

keen trail
#

i thought about functions
but its been a while i forgot how to go about that

devout fable
#

you can wrap the y input inside an ifs

weak cloud
devout fable
#

if menu > 4: ask for y, like that

keen trail
#

i finished my python course a couple months ago
do need a refresh tbh

devout fable
#

I mean < 4

keen trail
#

hrmmm

#

could work as well

but i think best way to hone my skills again is to go down the function route

weak cloud
#
if menu == 9:
  sys.exit()
if menu in [5,6,7,8]:
  x = float(input("single num"))
else:
  x = float(input("First num"))
  y = float(input("Second num"))
keen trail
#

this is the calculator i made during my course<, thought id maybe improve upon it but start anew

def file_handling(num1, opp, num2, answer):

    with open('equations.txt', 'a+') as file:

        file.write(f"{num1} {opp} {num2} = {answer} \n")
        file.flush()
        file.seek(0, 0)
        old_calculations = file.read()
        request = input("Would you like to see your previous equations? (yes/no) :")
        request_old = request.lower()

        if request_old == "yes":
            print(old_calculations)
        else:
            return


# function for all the calculations and exceptions to prevent problems from breaking the code
def calculator():

    print("\n")

    try:
        num1 = float(input("Please enter your first number: "))
        num2 = float(input("Please enter your second number: "))
    except ValueError as ERROR:
        print("Input given was not a number.\n")
        print(ERROR)
        print("Please try again: ")
        return

    opp = input("Please enter an operation (+; -; /; *) : ")

    if opp == "+":
        answer = num1 + num2
    elif opp == "-":
        answer = num1 - num2
    elif opp == "*":
        answer = num1 * num2
    elif opp == "/":
        try:
            answer = num1 / num2

        except ZeroDivisionError as ERROR:
            print("Equation was invalid. \n")
            print(ERROR)
            print("Please try again: ")
            return
    else:
        print("Input was invalid")
        return

    print(f"Answer = {answer}")
    return file_handling(num1, opp, num2, answer)


while True:
    calculator()
weak cloud
#

Definitely should practice it

#

And push yourself to make the calculator work like a regular calculator

keen trail
#

was also thinking about going down the UI route as well

weak cloud
#

e.g. We don't see a calculator asking for "Enter first number". We enter expressions like "1 + 2 / 2" and the calculator evaluates that expression

keen trail
#

would be nice to finally make something with a UI

weak cloud
#

Definitely also learn how to add a UI

#

GUI frameworks like Tkinter and Flet works

keen trail
#

going to go down backend development
but i like to mess around

#

would i define calculator as a class
then start adding all my little functions underneath that

weak cloud
#

If you know how to use classes, sure.

keen trail
#

ngl
that i also forgot

#

good thing i still have all the word docs that were my "lessons"

keen trail
weak cloud
#

tan, sin, cos

keen trail
#

that sounds like it would
be very complicated e'-e'

weak cloud
#

unit conversion

#

km to miles
kg to lb

#

celsius to fahrenheit

#

money exchange values

#

lots of things

stiff dust
#

And again my primary pedantry: kg is mass and lb is weight/force.

turbid tinsel
alpine troutBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.