Full calculator

 avatar
unknown
plain_text
a year ago
6.4 kB
7
Indexable
def draw_calc():
    penup()
    goto(-100,-160)
    pendown()
    color("#02055A")
    begin_fill()
    for i in range(2):
        forward(200)
        left(90)
        forward(300)
        left(90)
    end_fill()

#This function will draw the small screen
def draw_screen(): 
    color("#253DA1")
    penup()
    goto(80,80)
    left(90)
    pendown()
    begin_fill()
    forward(50)
    left(90)
    forward(160)
    left(90)
    forward(50)
    end_fill()

#This function will draw all the buttons 
def draw_button(x,y): 
    penup()
    color("dark blue")
    goto(x,y)
    begin_fill()
    for i in range(3):
        forward(30)
        left(90)
    end_fill()
    color("white")
    
#This function will label the buttons
def draw_number(x,y, label): 
    penup()
    goto(x,y)
    write(label, font=("Arial", 20), align="center")

#Speed and background
speed(0)
bgcolor("light grey")

#This calls the functions that create the base calculator
draw_calc()
draw_screen()

#Button 7
draw_button(-80,50)
draw_number(-65,25, 7)

#Button 8
draw_button(-5,50)
draw_number(-20,25, 8)

#Button 9
draw_button(40,20)
draw_number(25,25, 9)

#Button /
draw_button(55,20)
draw_number(70,25, "/")

#Button 4
draw_button(-80,10)
draw_number(-65,-15, 4)

#Button 5
draw_button(-5,10)
draw_number(-20,-15, 5)

#Button 6
draw_button(40,-20)
draw_number(25,-15, 6)

#Button x
draw_button(55,-20)
draw_number(70,-15, "x")

#Button 1
draw_button(-80,-30)
draw_number(-65,-55, 1)

#Button 2
draw_button(-5,-30)
draw_number(-20,-55, 2)

#Button 3
draw_button(40,-60)
draw_number(25,-55, 3)

#Button -
draw_button(55,-60)
draw_number(70,-55, "-")

#Button =
goto(1,-80)
color("Dark blue")
begin_fill()
for i in range(2):
    forward(40)
    right(90)
    forward(80)
    right(90)
end_fill()
color("white")
draw_number(-40,-110, "=")

#Button 0
draw_button(10,-80)
draw_number(25,-105, 0)

#Button +
draw_button(85,-80)
draw_number(70,-105, "+")

#Clickable interaction 
canvas=getscreen()
canvas.num1= ""
canvas.operator=""
canvas.num1_done= False
canvas.num2= ""
canvas.num2_done= False

#This function will solve the question inputted 
def solve_question(): 
    if canvas.operator == "+":
        answer = int(canvas.num1) + int(canvas.num2)
        write(canvas.num1 + "" + canvas.operator + "" + canvas.num2 +"="+  str(answer), font=("Arial", 15), align="right")
    if canvas.operator == "-":
        answer = int(canvas.num1) - int(canvas.num2)
        write(canvas.num1 + "" + canvas.operator + "" + canvas.num2 +"="+ str(answer),font=("Arial", 15), align="right")
    if canvas.operator == "x":
        answer = int(canvas.num1) * int(canvas.num2)
        write(canvas.num1 + "" + canvas.operator + "" + canvas.num2 +"="+ str(answer),font=("Arial", 15), align="right")
    if canvas.operator == "/":
        answer = int(canvas.num1) / int(canvas.num2)
        write(canvas.num1 + "" + canvas.operator + "" + canvas.num2 +"="+ str(answer),font=("Arial", 15), align="right")

#This function will help you to click and write numbers & symbols
def use_calculator(x,y):
    if canvas.num1_done == False:
        if x>-80 and x<-50 and y>10 and y<40:
            canvas.num1 += "7"
        elif x>-40 and x<-10 and y>10 and y<40:
            canvas.num1 += "8"
        elif x>0 and x<30 and y>10 and y<40: 
            canvas.num1+= "9"
        elif x>-80 and x<-50 and y>-30 and y<0:
            canvas.num1 += "4"
        elif x>-40 and x<-10 and y>-30 and y<0: 
            canvas.num1+= "5"
        elif x>0 and x<30 and y>-30 and y<0:
            canvas.num1 += "6"
        elif x>-80 and x<-50 and y>-70 and y<-40: 
            canvas.num1+= "1"
        elif x>-40 and x<-10 and y>-60 and y<-30:
            canvas.num1 += "2"
        elif x>0 and x<30 and y>-60 and y<-30: 
            canvas.num1+= "3"
        elif x>0 and x<30 and y>-110 and y<-80:
            canvas.num1 += "0"
        elif x>40 and x<70 and y>-110 and y<-90:
            canvas.num1_done = True
            canvas.operator ="+"
        elif x>40 and x<70 and y>-70 and y<-40:
            canvas.num1_done = True
            canvas.operator = "-"
        elif x>40 and x<70 and y>-30 and y<0:
            canvas.num1_done = True 
            canvas.operator ="x"
        elif x>40 and x<70 and y>10 and y<40:
            canvas.num1_done = True
            canvas.operator = "/"
        seth(0)
        draw_screen()
        penup()
        goto(80,80)
        color("white")
        pendown()
        write(canvas.num1 + "" + canvas.operator, font=("Arial", 15), align="right")
    
    elif canvas.num2_done == False: 
        if x>-80 and x<-50 and y>10 and y<40: 
            canvas.num2 += "7"
        elif x>-40 and x<-10 and y>10 and y<40:
            canvas.num2 += "8"
        elif x>0 and x<30 and y>10 and y<40: 
            canvas.num2+= "9"
        elif x>-80 and x<-50 and y>-30 and y<0:
            canvas.num2 += "4"
        elif x>-40 and x<-10 and y>-30 and y<0: 
            canvas.num2+= "5"
        elif x>0 and x<30 and y>-30 and y<0:
            canvas.num2 += "6"
        elif x>-80 and x<-50 and y>-70 and y<-40: 
            canvas.num2+= "1"
        elif x>-40 and x<-10 and y>-60 and y<-30:
            canvas.num2 += "2"
        elif x>0 and x<30 and y>-60 and y<-30: 
            canvas.num2+= "3"
        elif x>0 and x<30 and y>-110 and y<-80:
            canvas.num2 += "0"
        elif x>40 and x<70 and y>-110 and y<-90:
            canvas.num2_done = True
            canvas.operator ="+"
        elif x>40 and x<70 and y>-70 and y<-40:
            canvas.num2_done = True
            canvas.operator = "-"
        elif x>40 and x<70 and y>-30 and y<0:
            canvas.num2_done= True 
            canvas.operator ="x"
        elif x>40 and x<70 and y>10 and y<40:
            canvas.num2_done = True
            canvas.operator = "/"
        seth(0)
        draw_screen()
        penup()
        goto(80,80)
        color("white")
        pendown()
        write(canvas.num1 + "" + canvas.operator + "" + canvas.num2, font=("Arial", 15), align="right")

#This statement will give the result    
    if x>-80 and x<-20 and y>-110 and y<-80:
        canvas.num2_done = True
        seth(0)
        draw_screen()
        penup()
        goto(80,80)
        color("white")
        pendown()
        solve_question()

#Calling full function        
canvas.onclick(use_calculator)
Editor is loading...
Leave a Comment