How to make a calculator With Python [Calculator]

in utopian-io •  7 years ago  (edited)

1_PPIp7twJJUknfohZqtL8pQ.png

What Will I Learn?

  • You will learn Python looping and branching function
  • You will learn Exception in Python language

Requirements

  • python language
  • Python writing procedure

Difficulty

  • Intermediate

Tutorial Contents

Hello People, today I will teach you How to build tools with Python more precisely to make a calculator, why I said build tools? because of the plan I want to make this tool in the future with various features and teach you guys,

Okay now we start this tutorial.

  • Open your Notepad
  • Copy this source
def mtk():
    def daftar2():
        print ("") 
        print ("")
        print ("\tPanel Utopian")
        print ("\n============================")
        print ("")
        print ("\nChoose : ")
        for MataUang in ["1. Increment (+)","2. Reduction (-)","3. Multiplication (*)","4. Division (/)","5. Exit"]:
            print MataUang  
        print ("")        
        print ("\n============================")        
        
    def pilihan2():
    print ("") 
        p = input ("Input Your Choose : ")
   if p == 1:
            a = input("Input Your Value: ")
            b = input("Input Your Value: ")
            hasil = a + b
            print ("Your increment Result : "), hasil
            print ("\n============================")
            print ("")
            

        elif p == 2:
            a = input ("Input Your Value: ")
            b = input ("Input Your Value: ")
            hasil = a - b
            print ("Your Reduction Result : "), hasil
            print ("\n============================")
            print ("")
elif p == 3:
            a = input ("Input Your Value: ")
            b = input ("Input Your Value: ")
            hasil = a * b
            print ("Your Multiplication Result : "), hasil
            print ("\n============================")
            print ("")
            


        elif p == 4:
            a = input ("Input Your Value: ")
            b = input ("Input Your Value: ")
            hasil = a / b
            print ("Your Division Result : "), hasil
            print ("\n============================")
            print ("")
elif p==5:
            exit


    try:
        daftar2()
        pilihan2()
    except:
        print ("Number Only!!")

def daftar_utama():
    print ("")        
    print ("")
    print ("\tPanel Utopian")
    print ("\n============================")
    print ("")
    print ("\nChoose : ")
    for Menu_Utama in ["1. Calculators","2. Exit"]:
        print Menu_Utama
    print ("")
def pilihan_utama():
    q=input ("Input Your Choose : ")
    if q==1:
        mtk()
    elif q==2:
        exit

try:
    daftar_utama()
    pilihan_utama()
except:
   print ("Number Only!!")
def pilihan_utama():
    q=input ("Input Your Choose : ")
    if q==1:
        mtk()
    elif q==2:
        exit

try:
    daftar_utama()
    pilihan_utama()
except:
   print ("Number Only!!")

  • okay I will give you an explanation of the above code.

  • def mtk():
    Functions in Python are defined using the keyword def .
    After def no name recognition followed function with parameters between parentheses and end with colon :

on this tool def mtk(): for to marking the calculator menu.

for MataUang in ["1. Increment (+)","2. Reduction (-)","3. Multiplication (*)","4. Division (/)","5. Back","6. Exit"]:

print MataUang this code for looping function in python.
MataUang is a variable and will be called on print MataUang so it will output like this,

1.png

 p = input ("Input Your Choose : ")
        if p == 1:
            a = input("Input Your Value: ")
            b = input("Input Your Value: ")
            hasil = a + b
            print ("Your increment Result : "), hasil
            print ("\n============================")
            print ("")

on that source you can see at p = input ("Input Your Choose : ") variable p to save input from user and will be called at if p == 1:
which means if the variable p the user input in the form of 1 then the tool will run the program Increment, so also if user enter number 2 will run Reduction program, if the user enters a number that does not match the numbers on the menu then the program will stop and if the user input other than the number will issue a warning "Number Only !!" because we added Exception .

except:
        print ("Number Only!!")

and then variable a will save user input for the first number of data to be summed, and variable b will save user input for the second number of data to be summed, which will be summed to the hasil variable with called variable a and b.

def daftar_utama(): to mark the main menu.
at function def pilihan_utama(): you can see variable q for saved user input to select menu option between calculator or exit menu.

okay all now I'll show you the results of this tool
tk7r5butkakgqsk3ydzd.gif

okay guys I think until now I teach you about making tool with python. and I wanna to say thanks for your attention and thanks for read this tutorial sorry if I'm wrong on this tutorial,



Posted on Utopian.io - Rewarding Open Source Contributors

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Hi! I am a robot. I just upvoted you! I found similar content that readers might be interested in:
https://msdn.microsoft.com/en-us/library/ms525182(v=vs.90).aspx

the link you just submitted is an ASP language not a python language @ cheetah

  ·  7 years ago (edited)

Your contribution cannot be approved because it does not follow the Utopian Rules.

Violated Rule:

  • Tutorials must be technical instructions that teach non-trivial aspects of an Open Source project.

  • Design or video editing related tutorials, gameplay, simple on-screen instructions, ubiquitous functions (Save, Open, Print, etc.) or basic programming concepts (variables, operators, loops, etc.) will not be accepted.

My Opinion:

  • Trivial.

  • Basic programming concept.

  • A tutorial must be informative and explanatory, but also "tutor". This tutorial lacks "tutor"ing, and it is nothing more than "Do whatever I do." kind of tutorial.

  • I'll give a link to a good tutorial about Python. Of course, you don't have to show Python functions, you can show how to make something with Python. But "calculator" kind of concepts are too basic..

Here is the link:

https://utopian.io/utopian-io/@scipio/learn-python-series-intro

You can contact us on Discord.

[utopian-moderator]