ohm.py

Created by vnap0v

Created on May 26, 2025

1.39 KB

This ‘Ohms law helper’ allows you to type the two known variables with the unit added for example 5V or 0.1A or 10000ohm and the script automatically calculates the third variable which was unknown. Uses no imported libraries, also runs in CPython on a PC. Tested on the calculator using software version 23.2.6.


V=None;I=None;R=None
def determine(x):
  global V,I,R
  x=x.upper()
  if x.endswith("V"):
    x=x.replace("V","")
    V=float(x)
    return "voltage"
  elif x.endswith("A"):
    x=x.replace("A","")
    I=float(x)
    return "current"
  elif x.endswith("OHM"):
    x=x.replace("OHM","")
    R=float(x)
    return "resistance"
 
def ohmlaw():
  global V,I,R
  print("Ohms law helper")
  print("Enter voltage or current")
  print("or resistance")
  print("ex. 5V or 0.1A or 1000Ohm")
  answer=input()
  vartype1=determine(answer)
  if vartype1=="voltage":
      answer=input("Enter current or resistance\n")
      vartype2=determine(answer)
      if vartype2=="current":
        R=V/I
        print("Resistance: ",R,"Ohm")
      elif vartype2=="resistance":
        I=V/R
        print("Current:",I,"A")            
  elif vartype1=="current":
      answer=input("Enter voltage or resistance\n")
      vartype2=determine(answer)
      if vartype2=="voltage":
        R=V/I
        print("Resistance:",R,"Ohm")
      elif vartype2=="resistance":
        V=R*I
        print("Voltage:",V,"V")                     
  elif vartype1=="resistance":
      answer=input("Enter voltage or current\n")        
      vartype2=determine(answer)
      if vartype2=="voltage":
        I=V/R
        print("Current:",I,"A")
      elif vartype2=="current":
        V=R*I
        print("Voltage:",V,"V")  

ohmlaw()


    
    

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.