How to Build a Simple Calculator in Python - Step by Step 2

preview_player
Показать описание
How to Build a Simple Calculator in Python - Step by Step 2
Рекомендации по теме
Комментарии
Автор

personally what I've done, this was a useful tutorial

import math

print("Select an operation to perform: ")
print("1. ADD")
print("2. SUBTRACT")
print("3. MULTIPLY")
print("4. DIVIDE")
print("5. SQUARE ROOT")
print("6. RAISE TO POWER")

operation = input()

if operation == "1": # performs addition
num1 = input("Enter first number: ")
num2 = input("Enter second number: ")
print("The sum of this calculation is: " + str(int(num1) + int(num2)))

elif operation == "2": # performs subtraction
num1 = input("Enter first number: ")
num2 = input("Enter second number: ")
print("The sum of this calculation is: " + str(int(num1) - int(num2)))

elif operation == "3": # performs multiplication
num1 = input("Enter first number: ")
num2 = input("Enter second number: ")
print("The sum of this calculation is: " + str(int(num1) * int(num2)))

elif operation == "4": # performs division
num1 = input("Enter first number: ")
num2 = input("Enter second number: ")
print("The sum of this calculation is: " + str(int(num1) // int(num2)))

elif operation == "5": # performs square root
num = int(input("Enter number: "))
print("The square root is %f " %(float(math.sqrt(num))) )

elif operation == "6": # square a number
num = int(input("Enter number: "))
print("The power is %d " %(pow(num, 2)))

else:
print("Invalid Entry")

saintlavrent
Автор

on raise to the power add second line of code asking "what would you like the power to be" and instead of the (num1, 2) you can do (num1, num2)

Xidas_aventures
Автор

Full Calculator
-

import math
print("Select an operation to perform: (1 - 6")
print("1. ADD")
print("2. SUBTRACT")
print("3. MULTIPLY")
print("4. DIVIDE")
print("5. SQUARE ROOT")
print("6. RAISE TO POWER")

operation = input()

if operation == "1": # PERFORM ADDITION
num1 = input("Enter first number: ")
num2 = input("Enter second number: ")
print("The sum is " + str(int(num1) + int(num2)))
elif operation == "2": # PERFORM SUBTRACTION
num1 = input("Enter first number: ")
num2 = input("Enter second number: ")
print("The difference is " + str(int(num1) - int(num2)))
elif operation == "3": # PERFORM MULTIPLICATION
num1 = input("Enter first number: ")
num2 = input("Enter second number: ")
print("The product is " + str(int(num1) * int(num2)))
elif operation == "4": # PERFORM DIVISION
num1 = input("Enter first number: ")
num2 = input("Enter second number: ")
print("The result is " + str(int(num1) / int(num2)))
elif operation == "5": # SQUARE ROOT
num = int(input("Enter number: "))
print("The square root is %f " %(math.sqrt(num)) )
elif operation == "6": # RAISE TO POWER
num = int(input("Enter number: "))
print("The power is %d " %(math.pow(num, 2)) )
else:
print("Invalid Entry")

imtondoee
Автор

where’s the video of creating web interface and running this calculator into it?

towsifsadab
Автор

Thank you man this is so good and useful

marwaneahra-in
Автор

So, if someone is looking for a code which raises x to the power of n while x and n are inputs from the user, here is it:

elif operator == "6": # power of a number
number_1 = input("What's your base number?: ")
number_2 = input("What's your exponential number?: ")
print("The sum is:" + str(float(number_1) ** float(number_2)))

For me it didn't make sense to use pow( ) without a second number since it one, didn't work for me and two, I would like to user to choose their base and exponential number by themselves.

Overall, good video thank you.

rikka
Автор

Hi sir I need to develop a python program in anaconda to run an application like calculator or clock or any other applications if u have them in ur mind please suggest sir. Also how am I going to do it ?

memabe
Автор

Where is part three??
I guess he didn't upload it after 5 YEARS!!!!

ragavaviraji
Автор

Hy where is the follow up video to this one

anondebennett
Автор

I've inputed all but when I run my code, it's saying
Type error: float is required

divinepeace
Автор

WHO WANT PART THREE
like this comment
Edit: I never got more than 1 like please like this comment

ragavaviraji
Автор

Can any one tell me how to add the number in b/w the line like
print(The square of NUM is %f"

faizankhan-evit
Автор

Hi please i want to in box you if u dnt mind

BennTresor-yjmv
join shbcf.ru