我的Python程序编译时间足以获取输入,但不会处理输入

时间:2017-10-24 13:11:14

标签: python

def funcName():

存储用户最喜欢的主题的输入。

    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']

创建用户最喜欢的主题列表。

    favSubjects2 = []

存储用户最喜欢的主题。

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

将节目的主题与用户进行比较。

    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 个答案:

答案 0 :(得分:0)

这将生成IndexError,因为您无法分配到尚不存在的索引

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

要将这些项目存储在列表中,您应该使用以下

favSubjects2 = [in0, in1, in2]