Python Forum
[split] simple calculator
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[split] simple calculator
#1
I started learning python just a week ago and has been a very interesting experience. I have been able to write my first program in python, which is a program to make a simple calculator. I want to share it here for technical comments, contributions, and ideas on how I could make it better. I also join this forum only today so pardon me if I violate any respected rules for the use of this forum. I want to share the code here:

# Program to make a simple calculator
def calculator():
    num1 = float(input("Enter first number: "))
    op = input("Operator ")
    num2 = float(input("Enter second number: "))

    if op == "+":
       print(f"Answer: {add(num1, num2)}")
    elif op == "-":
        print(f"Answer: {subtract(num1, num2)}")
    elif op == "*":
        print(f"Answer: {multiply(num1, num2)}")
    elif op == "/":
        print(f"Answer: {divide(num1, num2)}")
    elif op == "**":
        print(f"Answer: {exponent(num1, num2)}")
    elif op == "%":
        print(f"Answer: {remainder(num1, num2)}")
    else:
        print("Invalid entry!")


# This function adds two numbers:
def add(n1, n2):
    return n1 + n2


# This function subtract two numbers:
def subtract(n1, n2):
    return n1 - n2


# This function multiplies two numbers:
def multiply(n1, n2):
    return n1 * n2


# This function divides two numbers:
def divide(n1, n2):
    return n1 / n2


# This function finds the exponents of two numbers:
def exponent(n1, n2):
    return n1 ** n2


# This function finds the remainder of two numbers:
def remainder(n1, n2):
    return n1 % n2


calculator()
Gribouillis write Aug-18-2022, 02:23 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Also this post would better fit in a new thread in the Code sharing part of the forum. Go to this page and hit the 'Post Thread' button to start your own thread.
Reply


Messages In This Thread
[split] simple calculator - by FelixLarry - Aug-18-2022, 02:24 PM
RE: [split] simple calculator - by rob101 - Aug-18-2022, 03:38 PM
RE: [split] simple calculator - by FelixLarry - Aug-18-2022, 04:07 PM
RE: [split] simple calculator - by rob101 - Aug-18-2022, 04:42 PM
RE: [split] simple calculator - by FelixLarry - Aug-19-2022, 12:41 AM
RE: [split] simple calculator - by menator01 - Aug-19-2022, 06:29 AM
RE: [split] simple calculator - by rob101 - Aug-19-2022, 10:07 AM
RE: [split] simple calculator - by FelixLarry - Aug-19-2022, 11:15 PM
RE: [split] simple calculator - by perfringo - Aug-19-2022, 10:46 AM
RE: [split] simple calculator - by FelixLarry - Aug-20-2022, 11:34 AM
RE: [split] simple calculator - by Yoriz - Aug-19-2022, 01:22 PM
RE: [split] simple calculator - by rob101 - Aug-20-2022, 12:15 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  a simple calculator Solstice 17 8,946 Mar-10-2019, 09:15 AM
Last Post: Ablazesphere

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020