Python Programming - Create a Simple Calculator Program - Updated 2021

preview_player
Показать описание
In this video you will will learn how to create a simple calculator that can add, subtract, multiply or divide depending upon the input from the user. (See the source further down)

In the tutorial video, we ask the user to choose the desired operation. Options 1, 2, 3 and 4 are valid. Two numbers are taken and an if...elif...else branching is used to execute a particular section. User-defined functions add(), subtract(), multiply() and divide() evaluate respective operations.

==Source Code==

# This program make a simple calculator that can add, subtract, multiply, and
# divide using function

# Define function (adds two numbers)

def add(x, y):
return x + y

# define function subtracts two numbers

def subtract (x, y):
return x - y

# define function multiplies two numbers

def multiply(x, y):
return x * y

# define function (divides two numbers)
def divide (x, y):
return x / y

print("Select Operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")

# Take input from the user

choice = input("Enter choice(1/2/3/4: ")

num1 = float(input("Enter First Number: "))
num2 = float(input("Enter Second Number: "))

if choice == '1':
print(num1, "+", num2,"=", add(num1,num2))

elif choice == '2':
print(num1, "-", num2, "=", subtract(num1,num2))

elif choice == '3':
print(num1, "*", num2, "=", multiply(num1,num2))

elif choice == '4':
print(num1, "/", num2, "=", divide(num1,num2))


else:
print("Invalid Input")

==EndCode==
Рекомендации по теме
Комментарии
Автор

Challenge for anyone who is learning Python. Once you've learned about "while" loops, make a calculator that asks for input until you type "=". If you want some added challenge, learn about the "try" command and try to prevent your program from crashing or ending when the user enters a letter. No shortcuts by giving the user an error or giving the result early.

It is a bit more complex than this video but it is a good challenge to learn more about Python. I just did it myself and it was a very useful exercise.

rendomstranger
Автор

if you have to input the numbers on the same line as the questions, try putting "\n" after the questions
ex. choice = input("Enter choice 1, 2, 3 or 4 \n")

sparkleface
Автор

print('SELECT OPERATION')
print('1.add')
print('2.sub')
print('3.div')
print('4.mult')

choice = input('enter your choice(1/2/3/4)')
num1 = float(input('enter a number'))
num2 = float(input('enter another number'))

if choice == '1':
print(num1 + num2)

if choice == '2':
print(num1-num2)


if choice == '3':
print(num1/num2)

if choice == '4':
print(num1*num2)


it works by that but little diff

thetopest
Автор

When you want to enter multiple no. Then ?

shubhamchowdhury
Автор

what if I want to take as a single input
for eg 5Addition11 must do 5+11 and print 16
similarly, 5multiply11 must do 5*11 and print 55
plz help

harijagadeeshiyer
Автор

Which website u used for making this coding

anshu_aru
Автор

i need this but with but ad an investment calculator

ziintle
Автор

when i try to input my choice it dosnt work when i type 1 for example and i press enter it goes to the line below

mohamedismail
Автор

Which application are you using for coding

arvindyadav
Автор

Bro u could have done it without Def function right?

fawaazahmed
Автор

a = input ('What do you want to do? Add, Subtract, Multiply, or Divide?: ')
b = input('Enter the first number: ')
c = input('Enter the second number: ')

if a = Add :
print (b + c)
What is wrong in this?!

spaazaim
Автор

This just doesn't work and is pretty bad without voice steps

basedmogy
Автор

must include voice which makes more clear

thetopest
Автор

how do i keep running the calculator with the user acceptance???

abusufiantamim
Автор

Doesn't work on python 2 just says syntax error on num1 for some weird reason and some other stupid stuff. Never trust any person like him for tech support, believe me!

richardwenham