binary_calculator.py

Created by wperez274

Created on February 01, 2023

3.88 KB

Add decimals and see binary code too. NOTE: You need to press [OK] key to start summing up the numbers.


# Binary plus decimal add
# By: Wilson
from math import *
from kandinsky import *
from kandinsky import fill_rect as F
from kandinsky import draw_string as STR
from ion import *
from ion import keydown as KEY
from time import *
from random import *

F(0,0,322,222,"white")
F(0,5,322,4,"red")
F(0,148,322,4,"red")

sleep(0.2)

while not KEY(KEY_OK) and not KEY(KEY_EXE):
  STR("[Left]/[Right] Arrows = +10/-10",0,20,"blue","white")
  STR("[Up]/[Down] Arrows = +1/-1",0,45,"blue","white")
  STR("BackSpace = Reset to value 0",0,70,"blue","white")
  STR("Keys [+]/[-] = Number +/- 100",0,95,"blue","white")
  STR("Keys [(]/[)] = Number +/- 10^3",0,120,"blue","white")
  STR("       ( PRESS [OK][EXE] )",0,190,"black","white")


print("\n",
"\n\n****************************\n",
"\n  You can print some",
"\n  numbers on the bottom",
"\n  panel by pressing [OK] key",
"\n\n  To get sum, press [EXE]",
"\n\n----------------------------\n",
"\n ( PRESS [OK] to Start )\n")

input()

num=0
y=67
the_sum=0
# screen top and bottom colors
# are ranomized just for
# the sake of visual candy.
screen=(randint(200,255),randint(200,255),randint(200,255))
panel=(randint(0,150),randint(0,150),randint(0,150))

default_f="black"
default_f2="white"
cursor_x=10

F(0,0,322,80,screen)
F(0,82,322,170,panel)  
F(0,0,322,2,"black")
F(0,35,322,2,"black")
F(0,80,322,2,"black")

while 1:
  b_code=bin(num)

  STR(str(num),10,12)
  STR(str(b_code),cursor_x,50,default_f,screen)
  
  if KEY(KEY_OK):
#    num=randint(0,255)
    F(2,2,322,78,screen)
    F(0,35,322,2,"black")
    sleep(.1)
  
  if KEY(KEY_UP):
    F(2,2,322,78,screen)
    F(0,35,322,2,"black")
    sleep(0.2)
    num+=1
  
  if KEY(KEY_DOWN):
    F(2,2,322,78,screen)
    F(0,35,322,2,"black")
    sleep(0.2)
    num-=1
  
  if KEY(KEY_PLUS):
    F(2,2,322,78,screen)
    F(0,35,322,2,"black")
    sleep(0.2)
    num+=100
  if KEY(KEY_MINUS):
    F(2,2,322,78,screen)
    F(0,35,322,2,"black")
    sleep(0.2)
    num-=100
  
  if KEY(KEY_RIGHTPARENTHESIS):
    F(2,2,322,78,screen)
    F(0,35,322,2,"black")
    sleep(0.2)
    num+=1000

  if num>4294967295:
    num=4294967295

  if KEY(KEY_LEFTPARENTHESIS):
    F(2,2,322,78,screen)
    F(0,35,322,2,"black")
    sleep(0.2)
    num-=1000
  
  if KEY(KEY_LEFT):
    F(2,2,322,78,screen)
    F(0,35,322,2,"black")
    sleep(0.2)
    num-=10

  if num<=0:
    num=0
  
  if KEY(KEY_RIGHT):
    F(2,2,322,78,screen)
    F(0,35,322,2,"black")
    sleep(0.2)
    num+=10
  
  if KEY(KEY_BACKSPACE):
    the_sum=0
    y=67
    screen=(randint(200,255),randint(200,255),randint(200,255))
    panel=(randint(0,150),randint(0,150),randint(0,150))
    F(0,82,322,170,panel)  
    F(2,2,322,78,screen)
    F(0,35,322,2,"black")
    num=0
    sleep(0.2)
  
  if KEY(KEY_OK) and y<17+20*8 or KEY(KEY_OK) and y<17+20*7:
    sleep(0.2)
    a=num
    the_sum+=num
    y+=20
    STR(str(a)+"="+str(bin(a)),10,y,choice([(156,169,107),(102,225,165),(190,209,143),(162,208,200),(150,228,202),(255,255,255),(0,255,0)]),"black")
  if y>17+20*8:
    STR("(MAX REACHED)",150,87)
    STR("[Backspace]=Clear",150,107,(randint(200,250),randint(200,250),randint(200,250)),"black")
    
  if KEY(KEY_EXE):
    STR("[Backspace]=Clear",150,107,(randint(200,250),randint(200,250),randint(200,250)),"black")
    STR("SUM: "+str(the_sum)+" = "+bin(the_sum),100,200,"black",(255,229,204))

  if KEY(KEY_ZERO):
    F(2,2,322,78,screen)
    F(0,35,322,2,"black")
    
    num=0    
  if KEY(KEY_ONE):
    F(2,2,322,78,screen)
    F(0,35,322,2,"black")
    sleep(0.5)
    num+=1    
    
  if KEY(KEY_TWO):
    F(2,2,322,78,screen)
    F(0,35,322,2,"black")
    sleep(0.5)
    num+=2
  if KEY(KEY_THREE):
    F(2,2,322,78,screen)
    F(0,35,322,2,"black")
    sleep(0.5)
    num+=3
  if KEY(KEY_FOUR):
    F(2,2,322,78,screen)
    F(0,35,322,2,"black")
    sleep(0.5)
    num+=4
  if KEY(KEY_FIVE):
    F(2,2,322,78,screen)
    F(0,35,322,2,"black")
    sleep(0.5)
    num+=5

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.