-1

I've made this simple program that uses a def function to output one of the components of a list. The component isn't being printed as specified in the code, which is really confusing me. Any help would be appreciated.

Members = ["fine", "Mine"]
def Helpers():
    print("Helpers who wanna work work work")
    for record in Members:
        if(record[0] == "fine"):
            print(record[1])
      
Helpers()

  • `for record in Members:` says *"for each element in the list `Members` set the variable `record` to the value of the element"*. Since your list is one-dimensional, then `record` will be a string. Therefore `record[0]` isn't right. Instead `if(record == "fine"):` is what you are looking for. – JNevill Apr 25 '22 at 15:01
  • `record` will be `"fine"` on the first iteration of your loop, and `"Mine"` on the second iteration. `record[0]` will therefore be `"f"` and `"M"`, respectively. You will note that *neither* of those values is equal to `"fine"`... – jasonharper Apr 25 '22 at 15:02
  • 1
    This is that situation you need to embed some debugging `print` statements in your code. print `record` in every iteration to see what it is, and what you except from `record[0]`. – S.B Apr 25 '22 at 15:12

1 Answers1

-1

your problem that you use index on record that take the first letter position

you should run withou index


Members = ["fine", "Mine"]
def Helpers():
    print("Helpers who wanna work work work")
    for record in Members:
        if(record == "fine"):
            print(record)
      
Helpers()

Beny Gj
  • 607
  • 4
  • 16