Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

First Project - Calculator

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()

🤝
Im guessing this is terminal based? Why not trying using TKinter (Python's built in GUI library) to make it more visually appealing🤝
(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 🤝


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by LSDJul 15, 2020 02:55 AM 2 195 Jul 15, 2020 02:55 AM
Last Post: greasyAlessandra
[C#] by RackenNov 8, 2019 12:59 PM 2 282 Nov 8, 2019 12:59 PM
Last Post: 0xDEAD10CC
[Python] by TISADec 2, 2017 09:03 AM 4 240 Dec 2, 2017 09:03 AM
Last Post: Gliese