2017-09-28 12 views
0

私はこのプログラムを持っている:このwhileループで私を助けることができますか?

n = [] 
m = [] 

name = input("Enter student's names (separated by using commas) : ") 
mark = input("Enter student's marks (separated by using commas) : ") 
(n.append(name)) 
(m.append(mark)) 

total = 0 
index = 0 
while index < len(name) : 
    print("name:",name[index],'mark:',mark[index]) 
    index +=1 

をし、それは次のように出てきた:

Enter student's names (separated by using commas) : a,s,d 
Enter student's marks (separated by using commas) : 1,2,3 
name: a mark: 1 
name: , mark: , 
name: s mark: 2 
name: , mark: , 
name: d mark: 3 

それだけでこのように出てくるにする方法:

Enter student's names (separated by using commas) : a,s,d 
Enter student's marks (separated by using commas) : 1,2,3 
name: a mark: 1 
name: s mark: 2 
name: d mark: 3 

答えて

1

あなたが名前を確認する必要がありsplit関数を使用して配列としてマークします。

name = input("Enter student's names (separated by using commas) : ") 
mark = input("Enter student's marks (separated by using commas) : ") 

name = name.split(',') 
mark = mark.split(',') 
total = 0 
index = 0 
while index < len(name) : 
    print("name:",name[index],'mark:',mark[index]) 
    index +=1 

出力

$ python3 test.py 
Enter student's names (separated by using commas) : as,we,re 
Enter student's marks (separated by using commas) : 1,2,3 
name: as mark: 1 
name: we mark: 2 
name: re mark: 3 

あなたは空のリストを維持したい場合は、ここにあなたが何をすべきかです:

n = [] 
m = [] 
name = input("Enter student's names (separated by using commas) : ") 
mark = input("Enter student's marks (separated by using commas) : ") 
(n.extend(name.split(','))) 
(m.extend(mark.split(','))) 
total = 0 
index = 0 
while index < len(n) : 
    print("name:",n[index],'mark:',m[index]) 
    index +=1 
+0

はい、それが仕事に...ありがとう....しかし、質問は最初に空のリストを作成するように頼みます...そして、私はコンマを取り除く方法を知らない –

+0

空リストnとmを作成してカンマ区切りの値を追加しようとしていると思いますか? –

+0

wowww ... it workssss ....ありがとうございました。 –

関連する問題