我已经在这个家庭作业问题上工作了一个星期,现在我不能把我的大脑放在看起来如此简单的东西上。我有4个列表,并试图根据用户输入的内容在for循环中从每个列表中删除特定的项目。问题是当我试图从列表中访问特定的项目时,它的结果是我想要的输出的倍数。我应该在作业问题中使用集合。我不想找任何人为我做这个问题,而是帮助我做错了什么。我的如何设置列表或for循环的问题?我应该使用元组吗?
# List items are ordered. so all of possition [0] is information for course1.
# Position [1] is for course2 and so on.
courseID = ['course1','course2','course3','course4'] #Provided Info
room = ['room1', 'room2', 'room3', 'room4'] #Provided Info
proffessor = ['teacher1', 'teacher2', 'teacher3', 'teacher4'] #Provided Info
text = ['text1', 'text2', 'text3', 'text4'] #Provided Info
userInput = input('Enter Course Number: ') #Get User Input
for i in courseID: #For loop to cycle through the info provided
if userInput == courseID[0]:
print('room: ',room[0])
print('professor: ',[proffessor[0]])
print('Text: ', text[0])
else:
if userInput == courseID[1]:
print('room: ',room[1])
print('professor: ',[proffessor[1]])
print('Text: ', text[1])
else:
if userInput == courseID[2]:
print('room: ',room[2])
print('professor: ',[proffessor[2]])
print('Text: ', text[2])
OUTPUT=
Enter Course Number: course1
room: room1
professor: ['teacher1']
Text: text1
room: room1
professor: ['teacher1']
Text: text1
room: room1
professor: ['teacher1']
Text: text1
room: room1
professor: ['teacher1']
Text: text1
字符串
正在寻找:
Enter Course Number: Course #
room: room #
professor: teacher
text: text
型
1条答案
按热度按时间kyvafyod1#
更简单地说,你可以在courseID中获取输入值的索引,然后使用该索引访问其他列表。这消除了if-else链。
字符串