Python Forum
TypeError: string indices must be integers
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TypeError: string indices must be integers
#3
Just seen this reply...
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
Reply


Messages In This Thread
RE: TypeError: string indices must be integers - by thecoziest - Sep-29-2019, 04:51 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: string indices must be integers, not str hanieh 4 98,562 Jan-04-2021, 05:13 AM
Last Post: delonbest
  please Help. TypeError: list indices must be integers, not str skamaaa01 1 4,455 Feb-04-2018, 12:33 PM
Last Post: Gribouillis
  create a 20 digit string, and cast to a list then add all the digits as integers nikhilkumar 2 6,460 Jul-19-2017, 04:53 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020