Last active
February 7, 2026 21:49
-
-
Save stpettersens/9cf1c1f05d007beb9b342dea979dedd7 to your computer and use it in GitHub Desktop.
Simple program to find resistance for a given resistivity in ohm metre on copper or aluminium wires given length and c.s.a.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| # Simple program to find resistance | |
| # Author: Sam Saint-Pettersen, Feb 2026 | |
| # R = pL/A for a given resistivity in ohm metre (i.e. pCu or pAl). | |
| import os | |
| import sys | |
| # Resistivities | |
| resists: dict[str,float] = { | |
| 'Cu': 1.72, | |
| 'Al': 26.5 | |
| } | |
| materials: dict[str,str] = { | |
| 'Cu': 'Copper', | |
| 'Al': 'Aluminium' | |
| } | |
| base = (10 ** -9) | |
| def cls(): | |
| _cls: str = 'cls' if os.name == 'nt' else 'clear' | |
| os.system(_cls) | |
| def find_resistance() -> int: | |
| p: float = 0 | |
| m: str = '' | |
| while (True): | |
| cls() | |
| print("Calculate resistance from length and c.s.a:") | |
| mat: int = int(input("Wire [1 = Copper (Cu) , 2 = Aluminium (Al), else quit]: ")) | |
| match mat: | |
| case 1: | |
| p = resists['Cu'] | |
| m = materials['Cu'] | |
| case 2: | |
| p = resists['Al'] | |
| m = materials['Al'] | |
| case _: | |
| break | |
| print(f"Wire is {m} (p = {p} * 10^-9 ohm metre)") | |
| length: int = int(input("Length of wire in metres (m), 0 is quit: ")) | |
| if length <= 0: | |
| break | |
| pL: float = ((p * base) * length) | |
| area: float = float(input("Enter c.s.a in mm squared (mm^2), 0 is quit: ")) | |
| if area <= 0: | |
| break | |
| print() | |
| print("R = pL/A") | |
| a: float|int = area | |
| if a % 1 == 0: a = int(a) | |
| area = (area * (10 ** -6)) | |
| print() | |
| print(f"(({p} * 10^-9 ohm metre) * {length} m) / ({a} * 10^-6 m)") | |
| # Calculate the resistance, rounding the result to 3 decimal places. | |
| resistance: float = (round((pL / area) * 1000) / 1000) | |
| print() | |
| print(f"R = {resistance} ohms") | |
| if resistance < 1: | |
| print('OR') | |
| print(f'R = {int(resistance * 1000)} milliohms') | |
| print() | |
| input("Press Enter key to continue.") | |
| print("Exiting...") | |
| return 0 | |
| if __name__ == "__main__": | |
| sys.exit(find_resistance()) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To use install Python and save this program (gist) to your computer. Run it on the command line or in Python's IDLE (comes with Python on Windows) or Thonny (for Linux) environment.