added a readme and gitignore

This commit is contained in:
Maddie H 2022-09-21 16:47:41 +01:00
parent cdbf30eeb3
commit 2553ea5548
No known key found for this signature in database
GPG Key ID: 64FAA9959751687D
3 changed files with 20 additions and 8 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
venv
.idea

6
README.md Normal file
View File

@ -0,0 +1,6 @@
# ![Resistor Symbol](https://upload.wikimedia.org/wikipedia/commons/c/c3/Resistor_symbol_IEC.svg) Simple Resistor Calculator
A college mini project.
## How to use
The program will ask you to enter 5 resistor band colors. It will then calculate the resistance and tolerance of the
resistor from the band colours that you have entered.

20
main.py
View File

@ -1,4 +1,4 @@
import json from json import load as json_load
import colours as c import colours as c
@ -6,7 +6,7 @@ def load_json(file_name):
# Open a json file called file_name # Open a json file called file_name
with open(file_name) as file: with open(file_name) as file:
# Load the json file into a dictionary # Load the json file into a dictionary
return json.load(file) return json_load(file)
def convert(value): def convert(value):
@ -29,8 +29,8 @@ def input_colours(bands):
colours = [] colours = []
while len(colours) < 5: while len(colours) < 5:
# Calculate a suffix for the number i.e. 2nd or 3rd # Calculate a suffix for the number i.e. 2nd or 3rd
suffixes = ["", "st", "nd", "rd", "th", "th"] suffixes = ["st", "nd", "rd", "th", "th"]
suffix = suffixes[len(colours) + 1] suffix = suffixes[len(colours)]
# Ask the user to input the colour of the resistor band # Ask the user to input the colour of the resistor band
colour = input(f"{c.cyan}What is the colour of the " colour = input(f"{c.cyan}What is the colour of the "
@ -41,6 +41,10 @@ def input_colours(bands):
# Only add the colour if it is valid # Only add the colour if it is valid
colours.append(colour) colours.append(colour)
elif colour == "exit":
print(f"{c.red}Exiting..{c.end}")
exit()
else: else:
# Otherwise, print an error message # Otherwise, print an error message
print(f"{c.red}Error: This colour does not exist.{c.end}") print(f"{c.red}Error: This colour does not exist.{c.end}")
@ -55,15 +59,15 @@ def calc_resistor(colours, bands):
tolerance = 0 tolerance = 0
for i, colour in enumerate(colours): for i, colour in enumerate(colours):
# Get the band value from # Get the band digit for the first 3 colours
if i < 3: if i < 3:
bands_value += str(bands[colour]["band"]) bands_value += str(bands[colour]["band"])
# Get the multiplier # Get the multiplier for the 4rd colour
elif i == 3: elif i == 3:
multiplier = bands[colour]["multiplier"] multiplier = bands[colour]["multiplier"]
# Get the tolerance # Get the tolerance for the 5th colour
elif i == 4: elif i == 4:
tolerance = bands[colour]["tolerance"] tolerance = bands[colour]["tolerance"]
@ -74,7 +78,7 @@ def calc_resistor(colours, bands):
def main(file_name): def main(file_name):
# Print the welcome message # Print the welcome message
print(f"{c.magenta}Resistor Calculator v0.1.0{c.end}") print(f"{c.magenta}Resistor Calculator{c.end}")
# Load the band information from the json file # Load the band information from the json file
bands = load_json(file_name) bands = load_json(file_name)