2017-05-22 15 views
-2

基本上,您輸入名稱並將它們保存到列表中。假設我輸入了「a,b,c,d和e」,打印完列表後,出現「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 
+0

爲什麼迪yiy wrute'name_list.next'? –

+5

我建議你學習Python中'='和'=='的區別 – SiHa

回答

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)。

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值。

然後,count04,你是否name等於輸入,然後將其追加到name_list

代替檢查平等name == raw_input(...),你想要的是分配輸入值到name

因此,您不能使用==,但=

你的循環應該是:

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 

現在,這裏是一個更Python的方式:

names_list = []  # there are more than one name in the list 
for _ in range(5):  # the loop index is not needed, so I use the anonymous underscore 
    name = raw_input("Enter the student's name: ") 
    names_list.append(name)