How to Build Tools With Python Language [Calculator]

in #utopian-io7 years ago (edited)

What Will I Learn?

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

Requirements

  • Basic python language
  • Python writing procedure

Difficulty

  • Intermediate

Tutorial Contents

Well 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, so keep on monitoring my contribution in utopian.

Okay now we start this tutorial.

  1. Open your Notepad
  2. 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!!")

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,

image.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
ezgif-3-e27a5d63e3.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, see you on the next tutorial:)



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

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

Congratulations @farahulfa! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of upvotes received

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!

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

There are new set of rules try to read them carefully for your future contributions.

  • 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.

  • It is very similar to a contribution that was already accepted here

You can contact us on Discord.
[utopian-moderator]

hei @rufans
My tutorials where wrong?
please tell me, where is my tutorial error?
If you can not programming language please you are out of "moderator",
I will report you to utopia through "discord"