2017-07-26 51 views
-4

我想添加與數組score2數組得分的結果。 我的功能打印分數有問題。你有關於我的問題的想法嗎?請。我應該創建另一個空陣列嗎?例如一個數組score3?如何添加2個數組?

非常感謝您的幫助。因爲我是學生,這是學校的練習。

更多信息 創建一個功能,其屬性從最小到最大。 (第一輪) 實施例: 澤馬具有分數 亨利具有分數 梅西具有6個分數

然後 創建其屬性仍然分數從最小到最大(第二輪)
實施例的功能: 亨利具有分數 澤馬具有分數 梅西具有6個分數

下面是結果: 澤馬分數 亨利6個分數 個梅西12分

name = []; 
goal = []; 
point = [2,4,6] 
score = [] 
goal2 = [] 
score2 = [] 


def demand(nb): 
    for i in range(nb): 
    name.append(str(input("Enter name n° " + str(i+1) + " please : "))) 
    while True: 
     var = int(input("Enter the number of goal (10-100) for " + name[i] + ": ")); 
     if var >=10 and var <=100: break 
     print(var, " error ! ") 
    goal.append(var) 

    return name, goal; 


def tri1(name, goal): 
    for i in range(len(name)-1,0,-1): 
    for j in range(i): 
     if name[j+1] < name[j]: 
     temponame = name[j] 
     tempogoal = goal[j] 
     name[j] = name[j+1] 
     goal[j] = goal[j+1] 
     name[j+1] = temponame; 
     goal[j+1] = tempogoal 

    return name, goal 


def printTri1(name, goal): 
    for i in range(len(name)): 
    print(name[i] + " \t " + str(goal[i])); 



def printPoint1(name, point, score): 
    for i in range(len(name)): 
    print("Name : " + name[i] + " Your score is of " + str(point[i]) + " : " + str(goal[i]) + " goals. ") 
    score.append(point[i]) 



def demand2(nb): 
    for i in range(nb): 
    print("Name " + name[i] + " - : "); 
    while True: 
     var = int(input("Enter the number of goal (10-100) for " + name[i] + ": ")); 
     if var >=10 and var <=100: break 
     print(var, " error ! ") 
    goal2.append(var) 

    return name, goal, goal2 


def Tris2(name, goal, goal2): 
    for i in range(len(name)-1,0,-1): 
     for j in range(i): 
     for k in range(j): 
      if name[k+1] < name[k]: 
      temponame = name[k] 
      tempogoal = goal[k] 
      tempogoal2 = goal2[k] 
      name[k] = name[k+1] 
      goal[k] = goal[k+1] 
      goal2[k] = goal2[k+1] 
      name[k+1] = temponame; 
      goal[k+1] = tempogoal; 
      goal2[k+1] = tempogoal2; 


def printTri2(name, goal, goal2): 
    for i in range(len(name)): 
    print(name[i] + " \t " + str(goal[i]) + str(goal2[i])); 



def printPoint2(name, point, score2): 
    for i in range(len(name)): 
    print("Name : " + name[i] + " Your score is of " + str(point[i]) + " : " + str(goal2[i]) + " goals. ") 
    score2.append(point[i]) 


def printScore(attaquant, score, score2): 
    for i in range(len(attaquant)): 
     print(attaquant[ i ] + "\t" + str(score[i]) + "\t" + str(score2[i])) 



name, goal = demand(3); 
tri1(goal, name); 
printTri1(name, goal) 
printPoint1(name, point, score) 


name, goal, goal2 = demand2(3) 
Tris2(goal2, name, goal) 
printPoint2(name, point, score2) 
printScore(score, score2) 
+0

所以,你有一個數組名稱和其他與分數,並你想分別與這兩個合併的數組? –

+3

目前還不清楚你想要什麼 – Ivan

+0

你好@ Ubdus薩馬德,是的請 – tamzoro

回答

0

如果我得到它的權利,你想「拼接」兩個數組在一起:namescore不使用任何字典/內置功能。這裏是你如何做到這一點:

name = ["Karim Benzema", "Thierry Henry", "Lionel Messi"]; 
score1 = [12, 5, 20]; 
score2 = [8, 2, 10]; 

# prepare the result array 
result = []; 

# loop from 0 to len(name,score) - 1 
for i in range(len(name)): 

    # append to result the string concatenation of name[i] and score[i] 
    result.append(name[i] + " " + str(score1[i]+score2[i])); 

# display result 
print(result) 

編輯: 下面的代碼工作:

def printScore(name, score1, score2): 
    result = []; 
    for i in range(len(name)): 
    result.append(name[i] + " " + str(score1[i] + score2[i])); 
    print(result) 


name = ["Karim Benzema", "Thierry Henry", "Lionel Messi"]; 
score1 = [12, 5, 20]; 
score2 = [8, 2, 10]; 
printScore(name, score1,score2) 

的完整代碼,see here

+0

謝謝,但我想添加數組得分和數組得分2 – tamzoro

+0

貝婁...我的功能幾乎是正確的:-(謝謝 – tamzoro

+0

你也有很多縮進問題,你也可以在'tr1()'中定義你的函數,出現錯誤信息請參閱我的編輯 – Ivan