quarter1 = [0, "1-Course1", "2-Course2", "3-Course3", "4-Course4", "5-Course5"]
quarter2 = [0, "1-Course1", "2-Course2", "3-Course3", "4-Course4", "5-Course5"]
pick_q = int(raw_input("Pick a quarter: "))
if pick_q == 1:
assignment = 0
courses = int(raw_input("How many courses would you like to enroll? "))
print quarter1
while assignment < courses:
course = int(raw_input("Please select the course you'd like to enroll into(1-5): "))
newlist = []
chosen_assignment = quarter1[course]
newlist.append(chosen_assignment)
assignment += 1
print newlist
所以我试图制作这个课程,让学生可以在特定季度内报名参加不同的课程。我只把2季度作为例子。
我遇到的问题是我想从学生选择的课程中创建一个新的列表,例如,如果他希望课程1,2和3,那么新的列表应该能够打印出#34;您已注册[Course1,Course2,Course3]&#34; 但是当我运行这个并尝试打印新列表时,只有用户在这种情况下输入的最后一个选项[Course3]而不是之前的其他选择。
它不一定要打印列表,但用户应该能够从原始列表中进行选择并收集此信息以创建新列表。我在列表中输入零,以便用户可以从列表索引1-5中选择一个数字。我是python的新手并试图弄明白这件事。提前谢谢!!
非常感谢任何其他推荐!!
答案 0 :(得分:0)
基本上,newlist变量在循环内反复初始化。你只需要在循环之外声明它。
quarter1 = [0, "1-Course1", "2-Course2", "3-Course3", "4-Course4", "5-ourse5"]
quarter2 = [0, "1-Course1", "2-Course2", "3-Course3", "4-Course4", "5-Course5"]
newlist = [] # Declare it outside
pick_q = int(raw_input("Pick a quarter: "))
if pick_q == 1:
assignment = 0
courses = int(raw_input("How many courses would you like to enroll? "))
print quarter1
while assignment < courses:
course = int(raw_input("Please select the course you'd like to enroll into(1-5): "))
chosen_assignment = quarter1[course]
newlist.append(chosen_assignment)
assignment += 1
print newlist