Python calculator program 🧮
HTML-код
- Опубликовано: 5 сен 2024
- #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")
I just wanted to tell you that please continue uploading courses/ classes ... maybe it won't be too much for you, but because of you and your videos I finally decide to start learning to code. Thank you
How do it in console like 5+4 give me 9 without make it like this
5
Enter
+
Enter
4
And allow the user to quit or perform another operation
bro has a great .pharaoh's first concubine?.
operator = input("Enter an operator (+ - * /): ")
if operator == "+" or operator == "-" or operator == "*" or operator == "/" :
num1 = float(input("Enter the 1st number: "))
num2 = float(input("Enter the 2nd number: "))
if operator == "+":
result = num1 + num2
elif operator == "-":
result = num1 - num2
elif operator == "*":
result = num1 * num2
elif operator == "/":
result = num1 / num2
print(round(result, 3))
else:
print(f"{operator} is not a valid operator")
tnx so much man
"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.
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)
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 !
I never thought of being a coder until I saw your videos, thank you so muck
*much*😅
@@charakamal2000 there's an edit comment fetus btw
feature*
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")
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("")
nice use of lists . i wanted to do something like that but was unsure of oding so
This won't give the result.
It will say, it's not a valid operator even after listing the operators when code's been run
This course has been incredible! Thank you!!
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)
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!!")
lambda better
but importing is out of the syllabus
bro wth dont add 50 lines of code for one calculator bruh
if u want to just write a caluclator with functions
Basic calculator but messy code 😅
I coded my self and now i know that there is an elseif command
😊
it is simple and easy to understand
Thank U brother for this Course, I have no words to explain it, btw Your voice is so Cool! 💗
The tutorial is good, but you also can do: print(eval(input("enter ")))
a=int(input())
if a%4==0:
print("kabisa")
else:
print("kabisa emas")
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.
He's already more popular than cs50…
But codecamp is a whole ahh organization, a single person can't beat a whole org's quantity of courses.
nice
I feel like there's a shorter code for this. But this is perfect for my simple and straightforward approach for things.
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.
Chatgpt will write entire code for you
eval()
I made this already :^)
import math
print("1 = +")
print("2 = -")
print("3 = *")
print("4 = /")
print("5 = Squareroot")
print("6 = Power")
print("7 = Circumference")
print("8 = Area circle")
print("9 = Hypotenuse")
operation = str(input("Choose your operation: "))
if operation == str(1):
number1 = float(input("Number 1: "))
number2 = float(input("Number 2: "))
result = number1 + number2
print(f"{number1} + {number2} = {result}")
elif operation == str(2):
number1 = float(input("Number 1: "))
number2 = float(input("Number 2: "))
result = number1 - number2
print(f"{number1} - {number2} = {result}")
elif operation == str(3):
number1 = float(input("Number 1: "))
number2 = float(input("Number 2: "))
result = number1 * number2
print(f"{number1} * {number2} = {result}")
elif operation == str(4):
number1 = float(input("Number 1: "))
number2 = float(input("Number 2: "))
result = number1 / number2
print(f"{number1} / {number2} = {result}")
elif operation == str(5):
sqrt = float(input("Enter the number for your squareroot: "))
result = round(math.sqrt(sqrt), 4)
print(f"The squareroot of {sqrt} is {round(result, 2)}")
elif operation == str(6):
base = float(input("Enter the base: "))
exp = float(input("Enter the exponent: "))
result = math.pow(base, exp)
print(f"{base}^{exp} = {round(result, 2)}")
elif operation == str(7):
radius = float(input("Enter the radius: "))
result = 2 * math.pi * radius
print(f"The circumference of the circle is: {round(result, 2)}")
elif operation == str(8):
radius = float(input("Enter the radius: "))
result = math.pi * math.pow(radius, 2)
print(f"The area of the circle is: {round(result, 2)}")
elif operation == str(9):
a = float(input("Enter the lenght of side A: "))
b = float(input("Enter the lenght of side B: "))
result = math.sqrt(math.pow(a, 2) + math.pow(b, 2))
print(f"The lenght of the Hypotenuse is: {round(result, 2)}")
else:
print("This option is not available!")
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?
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.
So happy 😁 you uploaded a video.
😅 I've been waiting....
Ive learned C++ from your videos, Python is just so easy
I have been interested in coding more after I did this code!
I watched all your earlier lessons,
And i stopped the video and made a calculator by myself,
I used ×,÷ instead of * and / and your code is also different than me
why we cant just:
else:
print(operator + " is not a valid operator")
Watched it. Liked it.
great job. straight to the point
thank you ur tutorials help me a lot
NICELY DONE...❤
This is fantastic
This is my calculator:
print("""+ is addition
- is subtraction
* is multiplication
/ is division
** is power
// is full division
% is the division's remainder
""")
which1 = input("Choose either +, *, /, -, %, // or ** to do your calculation")
number1 = float(input("First Number: "))
number2 = float(input("Second Number: "))
if which1 == '+':
print(number1 + number2)
elif which1 == '*':
print(number1 * number2)
elif which1 == '/':
print(number1 / number2)
elif which1 == '-':
print(number1 - number2)
elif which1 == '//':
print(number1 // number2)
elif which1 == '%':
print(number1 % number2)
elif which1 == '**':
print(number1 ** number2)
else:
print("Invalid Operator")
i dont think that would work
last time i checked you couldnt do print"hello
boi")
just saying :D
Thanks! You helped me finish my calculator! I used part where you can choose operation and everthing else came out of my brain. Thanks for helping me!
🤩🤩🤩
Calculator
------------------------
number1 = float(input("> 1st number: "))
number2 = float(input("> 2nd number: "))
accepted_operators = ["+", "-", "*", "/"]
operator = input("> operators (+ - * /): ")
while not operator in accepted_operators:
print("Incorrect operator!")
operator = input("> operators (+ - * /): ")
if operator in accepted_operators:
break
if operator == "+":
print("Result:", number1+number2)
elif operator == "-":
print("Result:", number1-number2)
elif operator == "*":
print("Result:", number1*number2)
else:
print("Result:", number1/number2)
------------------------
Hey Bro, LOVE YOUR VIDS!! can you tell me which one gives more value? the 12 hours vid or the python for beginners playlist!! PLEASE HELP
thanks Bro
Thank you for your video)
what happens when you divide 1/0 or some x/0 because i have a major problem in doing the operations when its some number over zero (x/0), can you help me please or maybe someone else in the comment sections who maybe come across such a problem and was able to debug or solve the problem it would be very helpful to me.
Explain your issue again pls
@@Cat-memes_official625When I attempt the operation 1/0 using the Python calculator code, it returns an error message. I struggled to debug this message and wanted the code to print "Undefined" for x/0 division operations rather than an error message. That's all I wanted.
@@currytut455 Think about what you've learned about if statements. If num2 is equal to 0, then...
@@Dinamight77 thanks i used some if statements and it worked, and i learnt a new trick of using try statements also.😁
I made this same program but with area,volume perimeter and arthmetic operation (+ - * / )
You made it a bit simple, maybe next video try to make it with functions, that would be a good video.
😍😍😍
thanks
operator = input("Enter an operator (+ - * /): ")
if operator != "+" and operator != "-" and operator != "*" and operator != "/":
print(operator + " is not a valid operator")
else:
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))
Here is my code where I got rid of the problem of having to enter numbers after not entering a correct operator. I was a little confused because I put or instead of and so I had to put it into ChatGPT to understand what I did wrong but it felt good to come up with this solution.
Thanks for your video. :D
done
Thanks cool vid helped me out alot
😃
Thank you Bro
How if its like choose a number and the number 1 is
Addition 2 is subtraction and 3 is multiplication and 4 is division?
I think that pizza would be a great operator :c
Bro......... perfect👌
How do you perform the next operation on the next previous calculated answer
what are f strings used for? i see them all the time but i dont understand what makes them different from a normal print
in f strings we can insert any variable that we have created without any errors and can perform manipulations easily
like
age=12
if we use print function like
print(f"the stan is {age} years old)
in this above line at that age placed in curly bracket will show number 12 if print function is executed
You're a life saver ❤️
# CALCULATOR PROGRAM
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 :(")
INPUT
Enter an operator (+ - * /): +
Enter the 1st number: 1
Enter the 2nd number: 1
OUTPUT
2.0
Huge disadvantage here... what if you want to subtract num2 by num1 instead of num1?
i did this instead
num_1 = float(input("Enter Your first number!: "))
num_2 = float(input("Enter Your second number!: "))
operator = input("Enter and Operator from /, +, - and *")
if operator == "*":
print(f"Your result is: {num_1 * num_2}")
elif operator == "+":
print(f"Your result is: {num_1 + num_2} ")
elif operator == "-":
print(f"Your result is: {num_1 - num_2} ")
elif operator == "/":
print(f"Your result is: {round(num_1 / num_2, 2)} ")
else:
print(f"{operator} isnt a valid operator!")
Hello BroCode, I need a bit of help.
When I type in the division symbol, nothing comes up but the other operators are fine. I double checked but (/ )was still not working
in this updated code it says: The result is : result instead of just giving the result
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("The result is: " + (str(round(result, 3))))
elif operator == "-":
result = num1 - num2
print("The result is: " + (str(round(result, 3))))
elif operator == "*":
result = num1 * num2
print("The result is: " + (str(round(result, 3))))
elif operator == "/":
result = num1 / num2
print("The result is: " + (str(round(result, 3))))
else:
print(f"{operator} is not a valid operator")
remove the str function lol
vize_notu = int(input("Vize notunuzu giriniz: "))
final_notu = int(input("Final notunuzu giriniz: "))
if 101 > vize_notu > -1:
num1 = vize_notu
else:
print("Geçerli bir vize notu giriniz.")
if 101 > final_notu > -1:
num2 = final_notu
else:
print("Geçerli bir final notu giriniz.")
toplam_not = (num1 * 40 / 100) + (num2 * 60 / 100)
if toplam_not >= 50:
print(f"Tebrikler! Sınıfı geçtiniz. Notunuz: {toplam_not} ")
elif toplam_not < 50:
print(f"Maalesef dersi geçemediniz. Notunuz: {toplam_not}")
thank you
What if the user input a string in temp? What can we add in the if statement?
.pharaoh's first concubine?..pharaoh's first concubine?..pharaoh's first concubine?.
when i run it and i enter enverything and press enter it closes help
Hey what prgrame software is he using
Hey, I have a doubt?
Why can't we "return" the result, instead of print?
At the end you said that unfortunately we still have to type in the numbers, but can't we put the else statement as an if statement and then put an else statement that does nothing?
i wrote this code:
operator = input("Enter an operator (+ - * /): ")
num1 = float(input("Enter the 1st number: "))
num2 = float(input("Enter the 2nd number: "))
valid_operators = ["+", "-", "*", "/"]
if operator not in valid_operators:
print(f"{operator} is not a valid operator")
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))
elif operator == "/":
result = num1 / num2
print(round(result, 3))
else:
print("")
i don't get why you still have to write the numbers before printing the "operator is not a valid operator"
@@hunin27 Not working , shows invalid , why did you use it as a list ? I removed the list and then it worked.
no way. bro this comment was 7 months ago. i am a front end dev now. :)@@sahtarwars8386
bruh the python console💀💀
whats wrong with it?
lindo
Mine calculator is better:
print(eval(input("Calculator")))
what can i do if i want once the operation is made to ask again for the operation?
Thank you guys
thx 4 vid bro !
first
by any chance could you tell me how to find this version of python? having a hard time finding it for whatever reason.
It's Pycharm. Don't know the version of Pycharm he uses.
@@Jo-March-Wandering-By thank you, i appreciate it.
it says Syntax error at line 2 of your code: invalid syntax
Thank you BC!
Please what app are you using?
PyCharm
nice name@@AnxiousFrenchFries
OK the program im using is not working but it is not your fault but idk what to do
Why are you reuploading?
But how do I loop it😢