mathphys68.py

Created by laigna

Created on March 14, 2026

6.16 KB

7 categories, 19 formulas total. Designed for grades 6–8 curriculum


# MathPhys 6-8 - NumWorks
# Created by Pärle Laigna - https://parle.laigna.com
# Arrows:select OK:choose Back:back
from kandinsky import fill_rect as F,draw_string as D
from ion import keydown as K
from time import sleep as Z
from math import pi,sqrt
SW,SH=320,222
BK=(0,)*3;WH=(255,)*3
BL=(50,110,230);TL=(0,190,190)
YL=(220,200,60);GN=(60,190,60)
DG=(120,)*3;HD=(30,)*3

okv=1 if K(4)or K(52) else 0

def okd():
 return K(4)or K(52)

def okp():
 global okv
 d=okd()
 if d and not okv:
  okv=1
  return True
 if not d:okv=0
 return False

def wup():
 while okd():Z(0.02)
 Z(0.12)

def menu(title,items,sub=False):
 sel=0;n=len(items)
 while True:
  F(0,0,SW,SH,BK)
  D(title,SW//2-len(title)*5,6,TL,BK)
  F(0,24,SW,2,(40,)*3)
  for i in range(n):
   y=34+i*22
   if i==sel:
    F(8,y,SW-16,20,BL)
    D("> "+items[i],14,y+2,WH,BL)
   else:
    D("  "+items[i],14,y+2,DG,BK)
  if sub:D("Back=return",100,SH-16,(60,)*3,BK)
  else:D("Back=quit",115,SH-16,(60,)*3,BK)
  while True:
   if K(1):sel=(sel-1)%n;Z(0.15);break
   if K(2):sel=(sel+1)%n;Z(0.15);break
   if okp():wup();return sel
   if K(17):return-1
   Z(0.04)

def inp(prompt):
 try:
  v=input(prompt)
  return float(v)
 except:return None

def show(lines):
 F(0,0,SW,SH,BK)
 D("RESULT",130,6,TL,BK)
 F(0,24,SW,2,(40,)*3)
 for i,ln in enumerate(lines):
  c=YL if i==len(lines)-1 else WH
  D(str(ln),14,34+i*20,c,BK)
 D("OK=back",130,SH-16,DG,BK)
 while True:
   if okp()or K(17):wup();return
   Z(0.04)

def f_rect():
 l=inp("Length: ")
 w=inp("Width: ")
 if l and w:
  show(["Rectangle","l="+str(l)+" w="+str(w),
   "Area = l x w","A = "+str(round(l*w,4)),
   "Perimeter = 2(l+w)","P = "+str(round(2*(l+w),4))])

def f_tri():
 b=inp("Base: ")
 h=inp("Height: ")
 if b and h:
  show(["Triangle","b="+str(b)+" h="+str(h),
   "Area = b x h / 2",
   "A = "+str(round(b*h/2,4))])

def f_circle():
 r=inp("Radius: ")
 if r:
  show(["Circle","r="+str(r),
   "Area = pi x r^2",
   "A = "+str(round(pi*r*r,4)),
   "Circumf = 2 x pi x r",
   "C = "+str(round(2*pi*r,4))])

def f_trap():
 a=inp("Side a (top): ")
 b=inp("Side b (bottom): ")
 h=inp("Height: ")
 if a and b and h:
  show(["Trapezoid",
   "a="+str(a)+" b="+str(b)+" h="+str(h),
   "Area = (a+b)/2 x h",
   "A = "+str(round((a+b)/2*h,4))])

def f_box():
 l=inp("Length: ")
 w=inp("Width: ")
 h=inp("Height: ")
 if l and w and h:
  show(["Box / Rectangular prism",
   "l="+str(l)+" w="+str(w)+" h="+str(h),
   "Volume = l x w x h",
   "V = "+str(round(l*w*h,4)),
   "Surface = 2(lw+lh+wh)",
   "S = "+str(round(2*(l*w+l*h+w*h),4))])

def f_cyl():
 r=inp("Radius: ")
 h=inp("Height: ")
 if r and h:
  show(["Cylinder","r="+str(r)+" h="+str(h),
   "Volume = pi x r^2 x h",
   "V = "+str(round(pi*r*r*h,4)),
   "Surface = 2*pi*r*(r+h)",
   "S = "+str(round(2*pi*r*(r+h),4))])

def f_speed():
 c=menu("Speed/Dist/Time",
  ["v = d / t  (find speed)",
   "d = v x t  (find distance)",
   "t = d / v  (find time)"],True)
 if c==0:
  d=inp("Distance: ");t=inp("Time: ")
  if d and t and t!=0:
   show(["Speed = distance / time",
    "d="+str(d)+" t="+str(t),
    "v = "+str(round(d/t,4))])
 elif c==1:
  v=inp("Speed: ");t=inp("Time: ")
  if v and t:
   show(["Distance = speed x time",
    "v="+str(v)+" t="+str(t),
    "d = "+str(round(v*t,4))])
 elif c==2:
  d=inp("Distance: ");v=inp("Speed: ")
  if d and v and v!=0:
   show(["Time = distance / speed",
    "d="+str(d)+" v="+str(v),
    "t = "+str(round(d/v,4))])

def f_dens():
 c=menu("Density",
  ["p = m / V  (find density)",
   "m = p x V  (find mass)",
   "V = m / p  (find volume)"],True)
 if c==0:
  m=inp("Mass: ");v=inp("Volume: ")
  if m and v and v!=0:
   show(["Density = mass / volume",
    "m="+str(m)+" V="+str(v),
    "p = "+str(round(m/v,4))])
 elif c==1:
  p=inp("Density: ");v=inp("Volume: ")
  if p and v:
   show(["Mass = density x volume",
    "p="+str(p)+" V="+str(v),
    "m = "+str(round(p*v,4))])
 elif c==2:
  m=inp("Mass: ");p=inp("Density: ")
  if m and p and p!=0:
   show(["Volume = mass / density",
    "m="+str(m)+" p="+str(p),
    "V = "+str(round(m/p,4))])

def f_pct():
 c=menu("Percentages",
  ["% of number  (x% of y)",
   "What %?  (x is ?% of y)",
   "Find whole (x is p% of ?)"],True)
 if c==0:
  p=inp("Percent: ");n=inp("Number: ")
  if p is not None and n is not None:
   show([str(p)+"% of "+str(n),
    "= "+str(round(p/100*n,4))])
 elif c==1:
  x=inp("Part: ");y=inp("Whole: ")
  if x is not None and y and y!=0:
   show([str(x)+" is what % of "+str(y)+"?",
    "= "+str(round(x/y*100,4))+"%"])
 elif c==2:
  x=inp("Part: ");p=inp("Percent: ")
  if x is not None and p and p!=0:
   show([str(x)+" is "+str(p)+"% of what?",
    "= "+str(round(x/(p/100),4))])

def f_pyth():
 c=menu("Pythagorean theorem",
  ["Find c (hypotenuse)",
   "Find a or b (leg)"],True)
 if c==0:
  a=inp("Leg a: ");b=inp("Leg b: ")
  if a and b:
   c2=sqrt(a*a+b*b)
   show(["a^2 + b^2 = c^2",
    "a="+str(a)+" b="+str(b),
    "c = sqrt("+str(round(a*a,2))+"+"+str(round(b*b,2))+")",
    "c = "+str(round(c2,6))])
 elif c==1:
  c2=inp("Hypotenuse c: ");a=inp("Leg a: ")
  if c2 and a and c2>a:
   b=sqrt(c2*c2-a*a)
   show(["b = sqrt(c^2 - a^2)",
    "c="+str(c2)+" a="+str(a),
    "b = "+str(round(b,6))])

def f_avg():
 print("\n"*20+"Enter numbers, empty=done")
 nums=[]
 while True:
  v=input("Value "+str(len(nums)+1)+": ").strip()
  if not v:break
  try:nums.append(float(v))
  except:pass
 if nums:
  s=sum(nums);n=len(nums)
  nums.sort()
  med=nums[n//2]if n%2 else(nums[n//2-1]+nums[n//2])/2
  show(["Average / Mean",
   "Count: "+str(n),
   "Sum: "+str(round(s,4)),
   "Mean: "+str(round(s/n,4)),
   "Median: "+str(round(med,4)),
   "Min: "+str(nums[0])+" Max: "+str(nums[-1])])

CATS=[
 "Geometry - Area",
 "Geometry - Volume",
 "Speed / Distance / Time",
 "Density",
 "Percentages",
 "Pythagorean theorem",
 "Average / Statistics"]
def run():
 while True:
  c=menu("MathPhys 6-8",CATS)
  if c<0:return
  if c==0:
   while True:
    s=menu("Area",["Rectangle","Triangle",
     "Circle","Trapezoid"],True)
    if s<0:break
    [f_rect,f_tri,f_circle,f_trap][s]()
  elif c==1:
   while True:
    s=menu("Volume",["Box / Prism",
     "Cylinder"],True)
    if s<0:break
    [f_box,f_cyl][s]()
  elif c==2:f_speed()
  elif c==3:f_dens()
  elif c==4:f_pct()
  elif c==5:f_pyth()
  elif c==6:f_avg()
run()

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.