使用List循环时遇到问题

时间:2017-05-22 11:30:43

标签: python

基本上,您输入名称并将其保存到列表中。我输入" a,b,c,d和e。打印完列表后,它会出现" a,a,a,a和a"

然后,当它询问学生是否已付款时,输入的值并不重要,该名称将不会被移至指定列表。

name_list = []
count = 0
name = raw_input("Enter the student's name: ")
while count < 5:                        #CHANGE BACK TO 45 
  name == raw_input("Enter the student's name: ")
  name_list.append(name)
  count = count + 1 
print "List full"
print name_list

paid_list = []
unpaid_list = []

for names in name_list:
  print "Has " + name + " paid? Input y or n: "
  input == raw_input()
  if input == "y":
    paid_list.append[input]
    name_list.next
  elif input == "n":
    unpaid_list.append[input]
    name_list.next

print "The students who have paid are", paid_list
print "The students who have not paid are", unpaid_list 

2 个答案:

答案 0 :(得分:0)

你可以尝试:

name_list = []
count = 0
name = raw_input("Enter the student's name: ")
while count < 5:                        
  name = raw_input("Enter the student's name: ")
  name_list.append(name)
  count = count + 1 
print "List full"
print name_list

paid_list = []
unpaid_list = []
for names in name_list:
  print "Has " + names + " paid? Input y or n: "
  input = raw_input()
  if input == "y":
    paid_list.append[input]
  elif input == "n":
    unpaid_list.append[input]

注意:raw_input()在python 3.x中不存在。而是使用:input()documentation)。

答案 1 :(得分:0)

您的填充循环是:

name_list = []
count = 0
name = raw_input("Enter the student's name: ")
while count < 5:                        #CHANGE BACK TO 45 
    name == raw_input("Enter the student's name: ")
    name_list.append(name)
    count = count + 1

您首先将通过raw_input收到的值分配给name

然后,对于从count0的{​​{1}},您检查4是否等于输入,然后将其附加到name

通过编写name_list而不是检查相等性,您想要的是输入值分配到name == raw_input(...)

因此,您不得使用name,而是==

你的循环应该是:

=

现在这是更多的Pythonic方式:

name_list = []
count = 0
name = raw_input("Enter the student's name: ")
while count < 5:                        #CHANGE BACK TO 45 
    name = raw_input("Enter the student's name: ")
    name_list.append(name)
    count = count + 1