mathphys910.py

Created by laigna

Created on March 14, 2026

8.63 KB

8 categories, 26 formulas total. Designed for grades 9–10 curriculum


# MathPhys 9-10 - 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,sin,cos,tan,asin,acos,atan,degrees,radians
SW,SH=320,222
BK=(0,)*3;WH=(255,)*3
BL=(50,110,230);TL=(0,190,190)
YL=(220,200,60);DG=(120,)*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:return float(input(prompt))
 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)

# === ALGEBRA ===
def f_quad():
 a=inp("a: ");b=inp("b: ");c=inp("c: ")
 if a and a!=0 and b is not None and c is not None:
  disc=b*b-4*a*c
  r=["ax^2 + bx + c = 0",
   "a="+str(a)+" b="+str(b)+" c="+str(c),
   "D = b^2-4ac = "+str(round(disc,6))]
  if disc>0:
   x1=(-b+sqrt(disc))/(2*a)
   x2=(-b-sqrt(disc))/(2*a)
   r+=["x1 = "+str(round(x1,6)),
    "x2 = "+str(round(x2,6))]
  elif disc==0:
   x1=-b/(2*a)
   r+=["x = "+str(round(x1,6))+" (double)"]
  else:
   re=-b/(2*a);im=sqrt(-disc)/(2*a)
   r+=["x1 = "+str(round(re,4))+"+"+str(round(im,4))+"i",
    "x2 = "+str(round(re,4))+"-"+str(round(im,4))+"i"]
  show(r)

def f_slope():
 x1=inp("x1: ");y1=inp("y1: ")
 x2=inp("x2: ");y2=inp("y2: ")
 if x1 is not None and y1 is not None and x2 is not None and y2 is not None:
  r=["Points: ("+str(x1)+","+str(y1)+") ("+str(x2)+","+str(y2)+")"]
  if x2!=x1:
   m=(y2-y1)/(x2-x1)
   b=y1-m*x1
   d=sqrt((x2-x1)**2+(y2-y1)**2)
   r+=["Slope m = "+str(round(m,6)),
    "y-int b = "+str(round(b,6)),
    "y = "+str(round(m,4))+"x + "+str(round(b,4)),
    "Distance = "+str(round(d,6))]
  else:r+=["Vertical line (x="+str(x1)+")"]
  show(r)

def f_prop():
 c=menu("Proportions",
  ["a/b = c/d  (find d)",
   "a/b = c/d  (find c)"],True)
 if c==0:
  a=inp("a: ");b=inp("b: ");cv=inp("c: ")
  if a and b and cv is not None and a!=0:
   show(["a/b = c/d","a="+str(a)+" b="+str(b)+" c="+str(cv),
    "d = b*c/a","d = "+str(round(b*cv/a,6))])
 elif c==1:
  a=inp("a: ");b=inp("b: ");d=inp("d: ")
  if a and b and d is not None and b!=0:
   show(["a/b = c/d","a="+str(a)+" b="+str(b)+" d="+str(d),
    "c = a*d/b","c = "+str(round(a*d/b,6))])

# === TRIGONOMETRY ===
def f_trig():
 c=menu("Trigonometry",
  ["sin/cos/tan (angle->ratio)",
   "arcsin/cos/tan (ratio->angle)",
   "Right triangle solver"],True)
 if c==0:
  a=inp("Angle (degrees): ")
  if a is not None:
   r=radians(a)
   show(["Angle = "+str(a)+" deg",
    "sin = "+str(round(sin(r),8)),
    "cos = "+str(round(cos(r),8)),
    "tan = "+str(round(tan(r),8))if abs(cos(r))>1e-10 else "tan = undefined"])
 elif c==1:
  v=inp("Ratio value: ")
  if v is not None:
   r=["Value = "+str(v)]
   if-1<=v<=1:
    r+=["arcsin = "+str(round(degrees(asin(v)),6))+" deg",
     "arccos = "+str(round(degrees(acos(v)),6))+" deg"]
   else:r+=["arcsin/arccos: |v| must be <=1"]
   r+=["arctan = "+str(round(degrees(atan(v)),6))+" deg"]
   show(r)
 elif c==2:
  print("\n"*5+"Right triangle: know 2 sides")
  a=inp("Side a (or 0=unknown): ")
  b=inp("Side b (or 0=unknown): ")
  c2=inp("Hypotenuse c (or 0=unknown): ")
  if a is not None and b is not None and c2 is not None:
   r=["Right triangle solver"]
   if a and b and not c2:
    c2=sqrt(a*a+b*b)
    r+=["c = "+str(round(c2,6))]
   elif a and c2 and not b:
    if c2>a:b=sqrt(c2*c2-a*a);r+=["b = "+str(round(b,6))]
   elif b and c2 and not a:
    if c2>b:a=sqrt(c2*c2-b*b);r+=["a = "+str(round(a,6))]
   if a and c2 and c2!=0:
    A=degrees(asin(a/c2))
    r+=["Angle A = "+str(round(A,4))+" deg",
     "Angle B = "+str(round(90-A,4))+" deg"]
   show(r)

# === MECHANICS ===
def f_force():
 c=menu("Force (F=ma)",
  ["F = m x a  (find force)",
   "m = F / a  (find mass)",
   "a = F / m  (find accel)"],True)
 if c==0:
  m=inp("Mass (kg): ");a=inp("Accel (m/s2): ")
  if m and a is not None:
   show(["F = m x a","m="+str(m)+" a="+str(a),
    "F = "+str(round(m*a,6))+" N"])
 elif c==1:
  f=inp("Force (N): ");a=inp("Accel (m/s2): ")
  if f is not None and a and a!=0:
   show(["m = F / a","F="+str(f)+" a="+str(a),
    "m = "+str(round(f/a,6))+" kg"])
 elif c==2:
  f=inp("Force (N): ");m=inp("Mass (kg): ")
  if f is not None and m and m!=0:
   show(["a = F / m","F="+str(f)+" m="+str(m),
    "a = "+str(round(f/m,6))+" m/s2"])

def f_energy():
 c=menu("Energy & Work",
  ["Ek = 1/2 mv^2 (kinetic)",
   "Ep = mgh (potential)",
   "W = F x d (work)",
   "P = W / t (power)"],True)
 if c==0:
  m=inp("Mass (kg): ");v=inp("Speed (m/s): ")
  if m and v is not None:
   show(["Kinetic energy","m="+str(m)+" v="+str(v),
    "Ek = 0.5 x m x v^2",
    "Ek = "+str(round(0.5*m*v*v,4))+" J"])
 elif c==1:
  m=inp("Mass (kg): ");h=inp("Height (m): ")
  if m and h is not None:
   g=9.81
   show(["Potential energy (g=9.81)",
    "m="+str(m)+" h="+str(h),
    "Ep = m x g x h",
    "Ep = "+str(round(m*g*h,4))+" J"])
 elif c==2:
  f=inp("Force (N): ");d=inp("Distance (m): ")
  if f is not None and d is not None:
   show(["Work","F="+str(f)+" d="+str(d),
    "W = F x d","W = "+str(round(f*d,4))+" J"])
 elif c==3:
  w=inp("Work (J): ");t=inp("Time (s): ")
  if w is not None and t and t!=0:
   show(["Power","W="+str(w)+" t="+str(t),
    "P = W / t","P = "+str(round(w/t,4))+" W"])

# === ELECTRICITY ===
def f_ohm():
 c=menu("Ohm's law + Power",
  ["V = I x R  (voltage)",
   "I = V / R  (current)",
   "R = V / I  (resistance)",
   "P = V x I  (power)",
   "P = I^2 x R",
   "P = V^2 / R"],True)
 vals=[(("Current I (A): ","Resistance R (ohm): "),lambda i,r:("V = I x R","V = "+str(round(i*r,6))+" V")),
  (("Voltage V (V): ","Resistance R (ohm): "),lambda v,r:("I = V / R","I = "+str(round(v/r,6))+" A")),
  (("Voltage V (V): ","Current I (A): "),lambda v,i:("R = V / I","R = "+str(round(v/i,6))+" ohm")),
  (("Voltage V (V): ","Current I (A): "),lambda v,i:("P = V x I","P = "+str(round(v*i,6))+" W")),
  (("Current I (A): ","Resistance R (ohm): "),lambda i,r:("P = I^2 x R","P = "+str(round(i*i*r,6))+" W")),
  (("Voltage V (V): ","Resistance R (ohm): "),lambda v,r:("P = V^2 / R","P = "+str(round(v*v/r,6))+" W"))]
 if 0<=c<6:
  pr,fn=vals[c]
  a=inp(pr[0]);b=inp(pr[1])
  if a is not None and b is not None and b!=0:
   f,r=fn(a,b)
   show([f,"= "+str(a)+", "+str(b),r])

# === KINEMATICS ===
def f_kinem():
 c=menu("Kinematics",
  ["v = v0 + at",
   "d = v0*t + 1/2*a*t^2",
   "v^2 = v0^2 + 2ad",
   "d = (v0+v)/2 * t"],True)
 if c==0:
  v0=inp("v0 (m/s): ");a=inp("a (m/s2): ");t=inp("t (s): ")
  if v0 is not None and a is not None and t is not None:
   show(["v = v0 + a*t",
    "v0="+str(v0)+" a="+str(a)+" t="+str(t),
    "v = "+str(round(v0+a*t,6))+" m/s"])
 elif c==1:
  v0=inp("v0 (m/s): ");a=inp("a (m/s2): ");t=inp("t (s): ")
  if v0 is not None and a is not None and t is not None:
   d=v0*t+0.5*a*t*t
   show(["d = v0*t + 0.5*a*t^2",
    "v0="+str(v0)+" a="+str(a)+" t="+str(t),
    "d = "+str(round(d,6))+" m"])
 elif c==2:
  v0=inp("v0 (m/s): ");a=inp("a (m/s2): ");d=inp("d (m): ")
  if v0 is not None and a is not None and d is not None:
   v2=v0*v0+2*a*d
   if v2>=0:
    show(["v^2 = v0^2 + 2ad",
     "v0="+str(v0)+" a="+str(a)+" d="+str(d),
     "v^2 = "+str(round(v2,4)),
     "v = "+str(round(sqrt(v2),6))+" m/s"])
   else:
    show(["v^2 = v0^2 + 2ad",
     "v^2 = "+str(round(v2,4)),
     "Object stops before d"])
 elif c==3:
  v0=inp("v0 (m/s): ");v=inp("v (m/s): ");t=inp("t (s): ")
  if v0 is not None and v is not None and t is not None:
   show(["d = (v0+v)/2 * t",
    "v0="+str(v0)+" v="+str(v)+" t="+str(t),
    "d = "+str(round((v0+v)/2*t,6))+" m"])

CATS=["Algebra - Quadratic",
 "Algebra - Slope & Line",
 "Algebra - Proportions",
 "Trigonometry",
 "Force (F=ma)",
 "Energy & Work & Power",
 "Electricity (Ohm's law)",
 "Kinematics (motion)"]
FNS=[f_quad,f_slope,f_prop,f_trig,
 f_force,f_energy,f_ohm,f_kinem]
def run():
 while True:
  c=menu("MathPhys 9-10",CATS)
  if c<0:return
  while True:
   FNS[c]()
   break
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.