Python calculator program 🧮

preview_player
Показать описание
#python #tutorial #code

operator = input("Enter an operator (+ - * /): ")
num1 = float(input("Enter the 1st number: "))
num2 = float(input("Enter the 2nd number: "))

if operator == "+":
result = num1 + num2
print(round(result, 3))
elif operator == "-":
result = num1 - num2
print(round(result, 3))
elif operator == "*":
result = num1 * num2
print(round(result, 3))
elif operator == "/":
result = num1 / num2
print(round(result, 3))
else:
print(f"{operator} is not a valid operator")
Рекомендации по теме
Комментарии
Автор

operator = input("Enter an operator (+ - * /): ")
num1 = float(input("Enter the 1st number: "))
num2 = float(input("Enter the 2nd number: "))

if operator == "+":
result = num1 + num2
print(round(result, 3))
elif operator == "-":
result = num1 - num2
print(round(result, 3))
elif operator == "*":
result = num1 * num2
print(round(result, 3))
elif operator == "/":
result = num1 / num2
print(round(result, 3))
else:
print(f"{operator} is not a valid operator")

BroCodez
Автор

"I'm not comfortable accepting donations for my work. Rather, I would like to encourage you to donate to a struggling family or a charity of your choosing."
He really said that.
Truly a bro we all need.

luvsk-rgxq
Автор

Fast way to make with modern python :
num1 = int(input("Enter 1st number: "))
num2 = int(input("Enter 2nd number: "))
calculate = input("Enter + or - or x or / : ")
match calculate:
case "+" :
print("The sum is :", num1 + num2)
case "-" :
print("The subtract is :", num1 - num2)
case "x" :
print("The product is :", num1 * num2)
case "/" :
print("The division is :", num1 / num2)

ERSites
Автор

i’d personally prefer it goes like this
the 1st number, then the operation and then the second number.
it seems more user friendly, but thank you for the tutorial !

Theebestest
Автор

I never thought of being a coder until I saw your videos, thank you so muck

charakamal
Автор

I just started watching your videos yesterday and I wish I had started a long time ago I am having too much fun.
operator = input("pick and operation (+, -, *, /) ")
if operator == "+":
num1 = float(input("what is the first number you would like to add to? "))
num2 = float(input("what is the second number you would like to add? "))
result = num1 + num2
print(f"{num1} plus {num2} equals {(round(result, 3))}")
elif operator == "-":
num1 = float(input("what is the first number you would like to subtract from? "))
num2 = float(input("what is the second number you would like to subtract by? "))
result = num1 - num2
print(f"{num1} minus {num2} equals {(round(result, 3))}")
elif operator == "*":
num1 = float(input("what is the first number you would like to multiply? "))
num2 = float(input("what is the second number you would like to multiply by? "))
result = num1 * num2
print(f"{num1} multiplied by {num2} equals {(round(result, 3))}")
elif operator == "/":
num1 = float(input("what is the first number you would like to divide? "))
num2 = float(input("what is the second number you would like to divide by? "))
result = num1 / num2
print(f"{num1} divided by {num2} equals {(round(result, 3))}")
else:
print("INVALID SELECTION! Please try again")

twigginbarrys
Автор

This course has been incredible! Thank you!!

Afran
Автор

Thank you for the lesson,
this is what I made today :D

operator = input("Enter an operator (+ - * /): ")

valid_operator = list ["+", "-", "*", "/"]
if operator not in valid_operator:
print(f"{operator} is not a valid operator")
exit()

num1 = float(input("Enter the 1st number: "))
num2 = float(input("Enter the 2nd number: "))

if operator == "+":
result = num1 + num2
print(round(result, 3))
elif operator == "-":
result = num1 - num2
print(round(result, 3))
elif operator == "*":
result = num1 * num2
print(round(result, 3))
elif operator == "/":
result = num1 / num2
print(round(result, 3))
else:
print("")

zahranurrohma
Автор

Thank U brother for this Course, I have no words to explain it, btw Your voice is so Cool! 💗

lookatrobot
Автор

import math
print("division (-)")
print("Addition (+)")
print("Multiplication (*)")
print("Division (/)")
print("Power (**)")
print("Square roots (sqr)")

operator = input("Enter one of the operators above: ")

if operator == "-":
x = float(input("Enter first value: "))
y = float(input("Enter a second value: "))
minus = x - y
print(f"It is equal: {minus} ")
elif operator == "*":
x = float(input("Enter first value: "))
y = float(input("Enter a second value: "))
times = x * y
print(f"It is equal: {times} ")
elif operator == "/":
x = float(input("Enter first value: "))
y = float(input("Enter a second value: "))
if y == 0 :
print("You can't divide by zero!!!")
else:
times = x / y
print(f"It is equal: {times} ")
elif operator == "**":
x = float(input("Enter first value: "))
y = float(input("Enter power value: "))
power = pow(x, y)
print(f"It is equal: {power} ")
elif operator == "sqr":
x = float(input("Enter value you want to square: "))
sqr = math.sqrt(x)
print(f"It is equal: {sqr} ")
else:
print("Enter a proper operator you dummy!!")

niceballz
Автор

a=input("enter your first numper ")

a1=input("enter your second numper ")

s1=int(a)+int(a1)
s2=int(a)/int(a1)
s3=int(a)*int(a1)
s4=int(a)-int(a)

calcul=input("calcule +, /, *, -")

if (calcul)=="+": print(s1)
if (calcul)=="/": print(s2)
if (calcul)=="*": print(s3)
if (calcul)=="-": print(s4)

Alajmi
Автор

I coded my self and now i know that there is an elseif command
😊

chrishagenstein
Автор

I predict, this channel will become more popular than cs50 or freecodecamp or any other coding channels in youtube if this kind of content continues to be uploaded.

julkarnayenshish
Автор

So happy 😁 you uploaded a video.
😅 I've been waiting....

gaddafisuleiman
Автор

Can you teach us how to code a calculator program that can calculate the math expression from an input string like 2 * (9 - 16/4), I will very appreciate it sir.

thosan
Автор

a=int(input())
if a%4==0:
print("kabisa")
else:
print("kabisa emas")

hcuznrs
Автор

I just picked up coding and I absolutely love your tutorials, thank you Bro for all the effort you put in!!
I have a question. Couldn't we have made only one print( ) in the end of all the if elif else? Something like print(f" {num1} {operator} {num2} = {result} ")
Wouldn't that work instead of having many print( ) statements for all the steps?

vaggelisgeorgiou
Автор

The tutorial is good, but you also can do: print(eval(input("enter ")))

srcafezin
Автор

Ive learned C++ from your videos, Python is just so easy

Priceygames
Автор

That was fun! I was trying to get a statement to print, where if the user input an invalid operator it would print it before asking for num1 & num2. Got to find out what command that is though.

Friday_