My Python program compiles long enough to take input, but won't process the input

0 Votes
    237 Views
def funcName():

Storing inputs for the user’s favourite subjects.

    in0 = input("Type your first favourite subject.")
    in1 = input("Type your second favourite subject.")
    in2 = input("Type your third favourite subject.")

    favSubjects = ['Physics', 'Computer_Science', 'Lunch']

Creating list of user’s favourite subjects.

    favSubjects2 = []

Storing user’s favourite subjects.

    favSubjects2[0] = in0
    favSubjects2[1] = in1
    favSubjects2[2] = in2

Comparing program’s subjects with the user’s.

    if(favSubjects[0] == favSubjects2[0] or favSubjects[0] == favSubjects2[1] or favSubjects[0] == favSubjects2[2]):
        print("Same!")
    elif(favSubjects[1] == favSubjects2[0] or favSubjects[1] == favSubjects2[1] or favSubjects[1] == favSubjects2[2]):
        print("Same!")
    elif(favSubjects[2] == favSubjects2[0] or favSubjects[2] == favSubjects2[1] or favSubjects[2] == favSubjects2[2]):
        print("Same!")

def main():
    funcName()

main()

1

Answers


  1. + -

    This will produce an IndexError as you cannot assign to indices that do not yet exist

    favSubjects2 = []
    favSubjects2[0] = in0
    favSubjects2[1] = in1
    favSubjects2[2] = in2
    

    To store these items in your list you should use the following

    favSubjects2 = [in0, in1, in2]
    
Please signup or login to answer this question.