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.
- 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!!")
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,
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
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