Hack Forums
[Python] First Project - Calculator - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] First Project - Calculator (/showthread.php?tid=4634533)



First Project - Calculator - georgeziozas - Jan 13, 2015

Sharing the code of my simple (for kids 😏) first program.

I just wrote it and im trying to find better ways to improve the code and to minimize the code lines.

Code
def choice():
    global a
    global b
    global r
    
    try:
        a = int(input("Give first number "))
        b = int(input("Give second number "))
        action = input("choose + , - , * , / ")


        if action == "+":
            addition()
        elif action == "-":
            substraction()
        elif action == "*":
            multi()
        elif action == "/":
            division()

    except ValueError:
         print ("Give ONLY Number")
         choice()
    
def substraction():
    r = (a - b)
    print (r)
def addition():
    r = (a + b)
    print (r)
def multi():
    r = (a * b)
    print (r)
def division():
    r = (a / b)
    print (r)

while True:
    
    choice()

🤝


* - maniacalfun - Jan 16, 2015

Im guessing this is terminal based? Why not trying using TKinter (Python's built in GUI library) to make it more visually appealing🤝


* - georgeziozas - Jan 17, 2015

(Jan 16, 2015 07:57 PM)maniacalfun Wrote:  Im guessing this is terminal based? Why not trying using TKinter (Python's built in GUI library) to make it more visually appealing🤝

I am aware of python's libraries :)
But the only reason i created it was to practice on code. nothing else.
Thanks for the advice though 🤝