# usr/bin/env python """ Title: "Resistor Calculator" Program Intent: Recieve resistor colors and return the value of the resistor. Creator: Andrew Eberhard Date: July 16, 2016 Completion: V1: July 23, 2016 - Completed main program, returned value V2: July 24, 2016 - Added input safety mechanisms, added comments V3: July 24, 2016 - Fixed bug in exception code allowing someone to enter after entering an integer in band_number while still wrong if next entered input is a string """ ############################################################################################################# ## Function: get_band ## Receives band inputs from user, calls conversion, determines resistance def get_band(number): band_1 = "color" band_2 = "color" band_3 = "color" resistance = " " if number == 4: band_1 = str(raw_input("\n\n\nPlease enter the color of the first band: ")) band_2 = str(raw_input("\nPlease enter the color of the second band: ")) band_1 = band_1.lower() band_2 = band_2.lower() value_1 = band_conversion(number, band_1) value_2 = band_conversion(number, band_2) resistance = str(value_1) + str(value_2) resistance = int(resistance) return resistance if number == 5: band_1 = str(raw_input("\n\n\nPlease enter the color of the first band: ")) band_2 = str(raw_input("\nPlease enter the color of the second band: ")) band_3 = str(raw_input("\nPlease enter the color of the third band: ")) band_1 = band_1.lower() band_2 = band_2.lower() band_3 = band_3.lower() value_1 = band_conversion(number, band_1) value_2 = band_conversion(number, band_2) value_3 = band_conversion(number, band_3) resistance = str(value_1) + str(value_2) + str(value_3) resistance = int(resistance) return resistance ############################################################################################################# ## Function: band_conversion ## Iterates through band_values dictionary until a match is found, returns the value of the color key def band_conversion(bands, xband): number_band = 0 count = 0 band_values = { "black": 0, "brown": 1, "red": 2, "orange": 3, "yellow": 4, "green": 5, "blue": 6, "violet": 7, "grey": 8, "white": 9 } while count <= 11: if bands == 4: for color, value in band_values.iteritems(): if xband == color: number_band = band_values[color] return number_band elif count <= 9: count = count + 1 continue elif count == 10: print "\n\tWARNING: The color '%s' is invalid." %(xband) xband = str(raw_input("\n\tATTENTION: Please enter the color of the band again: ")) xband = xband.lower() count = 0 break else: number_band = 0 if bands == 5: for color, value in band_values.iteritems(): if xband == color: number_band = band_values[color] return number_band elif count <= 9: count = count + 1 continue elif count == 10: print "\n\tWARNING: The color '%s' is invalid." %(xband) xband = str(raw_input("\n\tATTENTION: Please enter the color of the band again: ")) xband = xband.lower() count = 0 break else: number_band = 0 return number_band ############################################################################################################# ## Function: get_multiplier ## Receives multiplier band inputs from the user, calls the calc_multiplier function def get_multiplier(band): x10 = 0 band_multiplier = "" band_multiplier = str(raw_input("\nPlease enter the color of the multiplier band: ")) band_multiplier = band_multiplier.lower() x10 = calc_multiplier(band, band_multiplier) return x10 ############################################################################################################# ## Function: calc_multiplier ## Iterates through band_multiples dictionary until match is detected, returns the multiplier associated with the color value def calc_multiplier(number, color): count = 0 band_multiples = { "silver": 0.01, "gold": 0.1, "black": 1, "brown": 10, "red": 100, "orange": 1000, "yellow": 10000, "orange": 100000, "green": 1000000, "blue": 10000000, "violet": 100000000 } while count <= 12: for colour, multiplier in band_multiples.iteritems(): if color == colour: return multiplier elif count <= 10: count = count + 1 continue elif count == 11: print "\n\tWARNING: The multiplier band color '%s' is invalid." %(color) color = str(raw_input("\n\tATTENTION: Please enter the color of the multiplier band: ")) color = color.lower() count = 0 break else: multiplier = 0 return 0 ############################################################################################################# ## Function: get_tolerance ## Receives tolerance band input from the user, calls calc_tolerance function def get_tolerance(tband): band_tolerance = "" band_tolerance = str(raw_input("\nPlease enter the color of the tolerance band: ")) band_tolerance = band_tolerance.lower() tol = calc_tolerance(band_tolerance) return tol ############################################################################################################# ## Function: calc_tolerance ## Iterates over values_tolerance until match is found, returns the tolerance value of color def calc_tolerance(color): count = 0 values_tolerance = { "silver": 10, "gold": 5, "brown": 1, "red": 2, "green": 0.5, "blue": 0.25, "violet": 0.1 } while count <= 8: for tolerance, value in values_tolerance.iteritems(): if color == tolerance: return value elif count <= 6: count = count + 1 continue elif count == 7: print "\n\tWARNING: The tolerance band color '%s' is invalid." %(color) color = str(raw_input("\n\tATTENTION: Please enter the color of the tolerance band: ")) color = color.lower() count = 0 break else: color = 0 return 0 ############################################################################################################## ## Function: Main print ("\n\n\t\t\t \"Resistor Calculator\"" "\n\n\nThis program calculates a resistor's resistance using its given color code.\n") while True: ## Tests for input as an integer, returns error message if input is string try: program_go = int(raw_input("To continue enter 1, to quit enter 0: ")) except ValueError: print("\n\tERROR: Invalid input. The input must be an integer.\n") continue else: break while program_go > 0: value = 0 multiplier = 0 tolerance = 0 band_number = 0 while (band_number != 4 ) and (band_number != 5): while True: try: band_number = int(raw_input("\n\nPlease enter the number of color bands on the resistor: ")) except ValueError: print("\n\tERROR: Invalid input.") continue else: break if band_number != 4 and band_number != 5: print("\n\tERROR: Invalid Input") value = get_band(band_number) multiplier = get_multiplier(band_number) tolerance = get_tolerance(band_number) tot_resist = value * multiplier print("\n\n\nThe total resistance of the resistor is %s ohms with a tolerance of %s %%.\n") %(tot_resist, tolerance) while True: try: program_go = int(raw_input("\n\nTo calculate another resistor enter 1 to continue, or 0 to quit: ")) except ValueError: print("\n\tERROR: Invalid input. The number of bands must be an integer.") continue else: break print("\n\n\t\t\t PROGRAM TERMINATED\n\n") ############################################################################################################## """ Exception Code: http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response """