diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..82195aa --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +venv +.idea \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..c9dd186 --- /dev/null +++ b/README.md @@ -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. \ No newline at end of file diff --git a/main.py b/main.py index 9ca250b..310a540 100644 --- a/main.py +++ b/main.py @@ -1,4 +1,4 @@ -import json +from json import load as json_load import colours as c @@ -6,7 +6,7 @@ def load_json(file_name): # Open a json file called file_name with open(file_name) as file: # Load the json file into a dictionary - return json.load(file) + return json_load(file) def convert(value): @@ -29,8 +29,8 @@ def input_colours(bands): colours = [] while len(colours) < 5: # Calculate a suffix for the number i.e. 2nd or 3rd - suffixes = ["", "st", "nd", "rd", "th", "th"] - suffix = suffixes[len(colours) + 1] + suffixes = ["st", "nd", "rd", "th", "th"] + suffix = suffixes[len(colours)] # Ask the user to input the colour of the resistor band 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 colours.append(colour) + elif colour == "exit": + print(f"{c.red}Exiting..{c.end}") + exit() + else: # Otherwise, print an error message print(f"{c.red}Error: This colour does not exist.{c.end}") @@ -55,15 +59,15 @@ def calc_resistor(colours, bands): tolerance = 0 for i, colour in enumerate(colours): - # Get the band value from + # Get the band digit for the first 3 colours if i < 3: bands_value += str(bands[colour]["band"]) - # Get the multiplier + # Get the multiplier for the 4rd colour elif i == 3: multiplier = bands[colour]["multiplier"] - # Get the tolerance + # Get the tolerance for the 5th colour elif i == 4: tolerance = bands[colour]["tolerance"] @@ -74,7 +78,7 @@ def calc_resistor(colours, bands): def main(file_name): # 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 bands = load_json(file_name)