Sep-29-2019, 04:51 PM
Just seen this reply...
This is the whole program
This is the whole program
#The parameter is a resistor sequence. Example NKOD. #Returns TRUE if the length is equal to four & FALSE if its not. #Define sequence as string def check_length(sequence): """ (str) -> bool Return whether the length of the resistor sequence is valid. >>> check_length('NKOD') True >>> check_length('NKO') False """ if len(sequence) == 4: return True else: return False #The parameter is a resistor sequence with a valid length. #Returns TRUE only if sequence contains uppercase G,K,L,N,O,R,V,W,Y,Z #Anything else, such as lowercase will return FALSE def check_colours(sequence, valid_colours ="DKLNORWYZ"): """ (str) -> bool Return True if and only if the sequence contains the letters G,K,L,N,O,R,V,W,Y,Z. >>> check_colours('NKOD') True >>> check_colours('NKOF') False """ return all(color in valid_colours for color in sequence) #The parameter is a valid resistor sequence. #Strip off the last 2 characters and returns the number portion as a string def get_numbers(sequence): """ (str) -> str Return the numbers part of the sequence as a string. >>> get_numbers('NKOD') NK >>>get_numbers ('RRKS') RR """ return numbers #The parameter is the resistor sequence. #Return True if and only if the multipler (2nd last character) #conists of a DGKLNORSVY def is_valid_multiplier(sequence, valid_multi ="DGKLNORSVY"): return all(multi in valid_multi for multi in multiplier) #The parmeter is the resistor sequence. #Return TRUE if and only if the tolerance (last character) #consists of a DGLNRSVZ. def is_valid_tolerance(sequence, valid_tolerance="DGLNRSVZ"): return all(tole in valid_tolerance for tole in tolerance) #The first parameter is the two character string return earlier (numbers) #This function will convert the two characters to a number value & #return it. def calculate_raw_value(numbers): count = 0 band_values = {'K':0, 'N':1, 'R':2, 'O':3, 'Y':4,'G':5, 'L':6,'V':7, 'Z':8, 'W':9, } while count <= 11: for color, value in band_values.items(): if band_values[color] == numbers[value]: return numbers elif count <= 9: count = count + 1 continue elif count == 10: print("ERROR") #Program sequence = str(input("Enter the resistor sequence: ")) numbers = sequence[:2] multiplier = sequence[2] tolerance = sequence[3] #Test harness for testing purposes - will delete print(check_length(sequence)) print(check_colours(sequence)) print(get_numbers(sequence)) print(is_valid_multiplier(sequence)) print(is_valid_tolerance(sequence)) calculate_raw_value(numbers)I hope this gives you more clarity