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

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

Kindson The Tech Pro

5 лет назад

348,490 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

R7H4D
R7H4D - 04.11.2023 19:46

a=float(input("Enter first number:"))
b=float(input("Enter second number:"))
print("Sum=",a+b)
print("Subtraction=",a-b)
print("Product=",a*b)
print("Division=",a/b)
print("Remainder=",a%b)
Isnt this better and easy?

Ответить
Victor Mmaduegbochukwuaka
Victor Mmaduegbochukwuaka - 03.11.2023 18:24

Actually they also gave me as assignment

Ответить
Victor Mmaduegbochukwuaka
Victor Mmaduegbochukwuaka - 03.11.2023 18:15

Is good😂

Ответить
Mynameisandrew190
Mynameisandrew190 - 19.09.2023 19:00

Hey how do i get rid of the errors it doesnt let me start the thing

Ответить
Mohisa A T
Mohisa A T - 19.09.2023 11:49

Just wanna share my comments:

You can declare the numbers so you’ll have reusable function:

def get_nums():
x = float(input('Enter 1st number: '))
y = float(input('Enter 2nd number: '))
return x, y

and simply assign it to your operations function like this:

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

inside your loop you can do this instead:

x, y = get_nums()

so you don’t have to repeat it…

if operation == '1':
result = sum(x, y)

you can use fstring instead to make it simplified.
print(f'The Sum of {x} and {y} is {result}')


also put error handling on your division operation:

if y is equal to zero, you can print cannot divide by zero.

Overall, it' s really nice. Thanks for sharing your knowledge.

Ответить
Akbar Ali
Akbar Ali - 17.09.2023 08:31

Highlh recommended

Ответить
in and out
in and out - 26.08.2023 15:25

your teaching is brilliant please keep going

Ответить
Batuhan ALTUĞ
Batuhan ALTUĞ - 25.08.2023 15:28

pls paste the code next time

Ответить
Mykheal
Mykheal - 12.08.2023 05:53

1 line calculator: print(eval(input()))

Ответить
Stephen Bamidele
Stephen Bamidele - 15.07.2023 16:38

Can you please share your WhatsApp number with me

Ответить
Omar Shabo
Omar Shabo - 11.07.2023 21:32

I git an error at minus i needed to add an coma after the twxt xan someone explain?😅

Ответить
The Real One
The Real One - 01.07.2023 21:10

Do you deal With Software While Implementing These Programs?🤯

Ответить
Friendly Person
Friendly Person - 27.06.2023 16:22

This was such an amazing tutorial. Wonderful to follow and easy to understand! Keep up the great work.

Ответить
Hiking Feral
Hiking Feral - 02.04.2023 04:00

This is a basic calculator for beginners and he did a good job, pretty weird of people to be flexing more complex ones in the comments that involve learning about float, it's for b.e.g.i.n.n.e.r.s float is what stops you sink to beginner.

Ответить
Pavan gaming
Pavan gaming - 24.03.2023 18:09

Thanks again

Ответить
Tej Hari Ghimire
Tej Hari Ghimire - 11.03.2023 14:20

Very helpful bro keep on going -_-

Ответить
Vinícius Aguiar
Vinícius Aguiar - 09.03.2023 19:57

hi guys, i am trying to get to this programming thing, im looking for exercises for me to practice what i am learning, i just watch this video and i found a different way to code this calculator, and i would like to see if theres is something that could improve on this:

n1 = int(input('WHAT IS THE FIRST NUMBER? '))
n2 = int(input('WHAT IS THE SECOND NUMBER? '))
print ('SELECT AN OPERATION')
print('1. ADD')
print('2. SUBTRACT')
print('3. DIVIDE')
print('4. MULTIPLY')
OPERATION = input()
if OPERATION == '1':
RESULT = n1 + n2
elif OPERATION == '2':
RESULT = n1 - n2
elif OPERATION == '3':
RESULT = n1 / n2
elif OPERATION == '4':
RESULT = n1 * n2
else:
print('INVALID INPUT')

print(RESULT)

Ответить
Nolan
Nolan - 09.03.2023 06:58

elif is red for me. did I do something wrong?

operation = input()
if operation == 1:

elif operation == 2:

elif operation == 3:

elif operation == 4:

I'm using visual code studio if you ask me

Ответить
legiteh
legiteh - 30.01.2023 23:07

I think instead of using

operation = input()

if operation == “1”

You can use

operation = int(input()
If you want it to run without error instead of int use float() so if the user enters ‘1.5’ it’ll come back as invalid entry instead of error.
Also if you used int instead of float on num1 etc it will not read floats (numbers with a decimal)

Ответить
hindi knowledge tv
hindi knowledge tv - 20.01.2023 15:10

Hii I have also upload one short on my channel which also includes creation of calculator program in python in script mode.
First time I have ever made a video related to python language .
please watch it once 🙏🙏
plzz plzzz

Ответить
hindi knowledge tv
hindi knowledge tv - 20.01.2023 15:10

Hii I have also upload one short on my channel which also includes creation of calculator program in python in script mode.
First time I have ever made a video related to python language .
please watch it once 🙏🙏
plzz plzzz

Ответить
hindi knowledge tv
hindi knowledge tv - 20.01.2023 15:10

Hii I have also upload one short on my channel which also includes creation of calculator program in python in script mode.
First time I have ever made a video related to python language .
please watch it once 🙏🙏
plzz plzzz

Ответить
Pawan Fulara
Pawan Fulara - 19.01.2023 04:09

Bruh is just stuck with his own problems

Ответить
Esteban Godoy
Esteban Godoy - 16.01.2023 00:53

I´ve got the same errors, thanks for the help :)

Ответить
Ogochukwu Onwujekwe
Ogochukwu Onwujekwe - 07.01.2023 04:50

Thank you for this. I didn't know how to go about this but you just simplified it in 15 mins. Thank you so much

Ответить
Krish Varmora
Krish Varmora - 03.01.2023 14:16

dude i think u need to learn the calculator first

Ответить
Education with mr egg
Education with mr egg - 02.01.2023 11:32

im trying to build my own calculator in python but the + operator doesnt work properly, like im typing 1 as num1 and 3 as num2 but the program just prints out '13'

Ответить
Atomy Nepal
Atomy Nepal - 08.12.2022 13:42

3 and 4 code are enterchange

Ответить
Just some guy who does random content lmao
Just some guy who does random content lmao - 25.11.2022 14:11

Thank you, you made it sooooo simple❤❤😊

Ответить
IQ07
IQ07 - 19.11.2022 21:12

thank you

Ответить
walbx_
walbx_ - 16.11.2022 14:27

I just start to study Phyton this days but... at first, you dont need FIVE prints, right? I think if you just write with " " " should works; something like this:
print("""Select an operation to perfom:
1. Add
2. Subtract
3. Multiply
4. Divide
""")

Ответить
Rishi Dubey
Rishi Dubey - 05.11.2022 21:13

hllo

Ответить
mïlane
mïlane - 18.10.2022 23:16

Okay but where's the calculator u built tho ?

Ответить
INDUSTRIAL ELECTRICAL BD
INDUSTRIAL ELECTRICAL BD - 17.10.2022 11:18

Nice
Thanks a lot

Ответить
Ralf Kjapsna
Ralf Kjapsna - 22.09.2022 15:21

How to import fraction to this calculator?

Ответить
Randall Charles Tucker (MrCySec)
Randall Charles Tucker (MrCySec) - 17.09.2022 21:25

a bit different.

while True:

print("addition 1:")
print("subtract 2:")
print("multiply 3:")
print("division 4:")

operation = input("Enter Number 1-4: ")

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

if operation == "1":
print(num1, "+", num2, "=", (num1+num2))
elif operation == "2":
print(num1, "+", num2, "=", (num1-num2))
elif operation == "3":
print(num1, "+", num2, "=", (num1*num2))
elif operation == "4":
print(num1, "+", num2, "=", (num1/num2))
else:
print("invalid submission")

Ответить
haraaldidegdsenscientist
haraaldidegdsenscientist - 11.09.2022 11:23

thank you

Ответить
mustafa ahmed
mustafa ahmed - 09.09.2022 13:21

Thank you so much ☺️

Ответить
Jasonlonge
Jasonlonge - 07.09.2022 11:00

can you say me what i did wrong? i have 21 errors

rint("Select an operation to perform: ")
print("1. ADD")
print("2. SUBTRACT")
print("3. MULTIPLY")
print("4. DIVIDE")

operation = input()

if operation == "1":
num1 = input("Enter first number: ")
num2 = input("Enter second number: ")
print("The sum is " + str(int(num1) + (int(num2)))
elif operation == 2:
num1 = input("Enter first number: ")
num2 = input("Enter second number: ")
print("The sum is " + str(int(num1) - (int(num2))))
elif operation == 3:
num1 = input("Enter first number: ")
num2 = input("Enter second number: ")
print("The sum is " + str(int(num1) * (int(num2))))
elif operation == 4:
num1 = input("Enter first number: ")
num2 = input("Enter second number: ")
print("The sum is " + str(int(num1) / (int(num2))))
else:
print("Invalid Entry")

Ответить
glowria_balla
glowria_balla - 03.09.2022 15:13

I keep getting invalid syntax anytime I run my code

Ответить
Oluwagbemiga Babatunde
Oluwagbemiga Babatunde - 03.09.2022 13:28

This is really great. I like the way you intentionally made those mistakes to teach us what could cause any error.

You're a good teacher.

Ответить
Cosmo Blaze
Cosmo Blaze - 03.09.2022 08:05

Bruh I need a calculator programm which can accept multiple inputs

Ответить
MoonchildSnjs
MoonchildSnjs - 02.09.2022 10:16

Thank you so much!

Ответить
atoz
atoz - 30.08.2022 20:18

Thanks brother. (Channel subscribed)

Ответить
CWP(Code With Prince)
CWP(Code With Prince) - 29.08.2022 17:13

x = float(input("ENTER FIRST NUMBER :"))
opreater = input("enter operater (+,-,*,/) :")
y = float(input("ENTER SECOND NUMBER :"))

if opreater == "+":
print(x + y)
elif opreater == "-":
print(x - y)
elif opreater == "*":
print(x * y)
elif opreater == "/":
print(x / y)
else:
print("invalid syntex")

One simplest calculator 😃😃

Ответить
Rex Daniel Navarro
Rex Daniel Navarro - 28.08.2022 16:13

Thanks!

Ответить
Pro Coder
Pro Coder - 26.08.2022 14:24

i can code in 1 line
while True: print(f"ANS : {eval(input('> '))}")

Ответить
Hrishu K
Hrishu K - 12.08.2022 10:16

And the sum shows "45"😂

Ответить
Ben Que
Ben Que - 03.08.2022 15:20

Nice video, I did it
Btw how can I make it reusable

Ответить
Dusty
Dusty - 27.07.2022 16:34

how can you fix this?

Ответить