How add 2 arrays? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: How add 2 arrays? (/thread-4150.html) Pages:
1
2
|
RE: How add 2 arrays? - Augustin1340 - Jul-27-2017 Sorry…. How distribute items of the array "points" according the number of goal ? For example: Henry scored 20 goals ; Owen scored 16 goals and Drogba scored 12 goals. Then make the same thing with the variable goal2 I will wish to get this as fin result Henry will have 10 points (6 + 4) Owen will have 10 points (4 + 6) Drogba will have 4 points (2+2) def printPoint1(name, goal1, goal2): score1 = [] score2 = [] for i in range(len(name)): print("name : " + name[i] + " : " + str(goal1[i] + (goal2[i])) + " goals " ) score1.append(point[i]) score2.append(point[i]) print(score1,score2) name = ["Henry", "Owen", "Drogba"]; point = [2,4,6] goal1 = [20,16, 12] goal2 = [6,8,5] printPoint1(name, goal1, goal2,)[/i][/i][/i][/i][/i] RE: How add 2 arrays? - nilamo - Jul-27-2017 (Jul-27-2017, 01:56 PM)Augustin1340 Wrote: For example: We can't really talk about code until the input/output makes sense. And those numbers don't make sense. If you score 20 goals, how do you have only 10 points? Where are 6 and 4 coming from? Is a goal only worth partial points? RE: How add 2 arrays? - Augustin1340 - Jul-27-2017 (Jul-27-2017, 03:50 PM)nilamo Wrote: We can't really talk about code until the input/output makes sense. And those numbers don't make sense. It's a ranking between 3 soccer players only. I must to compare goals then I give points according the number of goals. Somebody who scored 5 goals will have 2 points. Another who scored 10 goals will have 4 points and another who scored 11 goals will have 6 points. In fact, it's from smallest to biggest RE: How add 2 arrays? - nilamo - Jul-27-2017 So it's based on ranking? Most goals = 6 points, least goals = 2 points, in between = 4 points? RE: How add 2 arrays? - Augustin1340 - Jul-27-2017 (Jul-27-2017, 06:58 PM)nilamo Wrote: So it's based on ranking? Most goals = 6 points, least goals = 2 points, in between = 4 points? Yeeesssss, It's exact I want me base on my code please... I blocke in the function printFinal() Now; I will wish to add up each point concerning players. Drogba 6 points Owen 10 points Henry 8 points Sorry, I have to put entirely my code if not impossible to understand def printOne(name, goal1): score1 = [] for i in range( len( name) ): print("\t\t\t\t" + name[ i ] + "\t " + str(point[i]) + " point - \t" + str( goal1[i]) + " buts") score1.append(point[i]) print(score1) def trisGoal1(name ,goal1): for i in range( len(name)-1, 0, -1 ): for j in range(i): if name[j+1] < name[ j ]: FlagParti = name[ j ] FlagLong = goal1[ j ] name[ j ] = name[ j + 1 ] goal1[ j ] = goal1[ j +1 ] name[ j + 1] = FlagParti goal1[ j + 1 ] = FlagLong def printSeconde(name, goal2): score2 = [] for i in range( len( name) ): print("\t\t\t\t" + name[ i ] + "\t " + str(point[i]) + " point - \t" + str( goal2[i]) + " buts") score2.append(point[i]) print(score2) def trisGoal2(name , goal2): for i in range( len(name)-1, 0, -1 ): for j in range(i): if name[j+1] < name[ j ]: FlagParti = name[ j ] FlagLong = goal2[ j ] name[ j ] = name[ j + 1 ] goal2[ j ] = goal2[ j +1 ] name[ j + 1] = FlagParti goal2[ j + 1 ] = FlagLong def printFinal(name, score1, score2): for i in range( len( name) ): print("\t\t\t\t" + name[ i ] + "\t " + " point - \t" + str( score1[i] + score2[i]) ) name = ["Henry", "Owen", "Drogba"]; point = [2,4,6] goal1 = [18,12,6] goal2 = [6,8,5] trisGoal1(goal1, name) printOne(name, goal1) trisGoal2(goal2, name) printSeconde(name, goal2) printFinal(name, point) RE: How add 2 arrays? - Augustin1340 - Jul-30-2017 Hello, Please help me... How sort correctly the final ranking according the first round and the second round ? For the ranking final the result must be this: Drogba 6 points 11 buts Henry 8 points 14 buts Owen 10 points 14 buts In my terminal there is a problem with points ??? I think my function sortFinal() is not correct ? I don't know to do this alone.... Here is my code # --- Step 1/ Print names + points and goals def FirstRound(name, goal, score1): for i in range(len(name)): score1.append(point[i]) print("Name : " + name[i] + " \t " + str(score1[i]) + " points " + str(goal[i]) + " goals ") return score1 def SecondRound(name, goal, score2): for i in range(len(name)): score2.append(point[i]) print("Nom : " + name[i] + " \t " + str(score2[i]) + " points " + str(goal[i]) + " goals ") return score2 # --- Etape 2/ Sort points according goals def Sort(name, goal1, goal2, pts): swap = True while swap: swap = False for i in range(len(name) - 1): if goal1[i + 1] < goal1[i]: name[i], name[i + 1] = name[i + 1], name[i] goal1[i], goal1[i + 1] = goal1[i + 1], goal1[i] goal2[i], goal2[i + 1] = goal2[i + 1], goal2[i] swap = True return name, goal1, goal2, pts # --- Step 3/ Print the number of points def printFinal(name, scoreFinal): for i in range(len(name)): scoreFinal.append(str(score1[i] + score2[i])) print("Nom : " + name[i] + " \t " + (scoreFinal[i]) + " points " ) return scoreFinal print(scoreFinal) # --- Step 4/ Sort according the number of points (from smallest to biggest ) def sortFinal(name, scoreFinal): swap = True while swap: swap = False for i in range(len(name) - 1): if scoreFinal[i + 1] < scoreFinal[i]: name[i], name[i + 1] = name[i + 1], name[i] swap = True return name, scoreFinal name = ["Henry", "Owen", "Drogba"] score1 = [] score2 = [] scoreFinal = [] goal1 = [10,8,6] goal2 = [4,6,5] point = [2,4,6] print(" First round : ") print(" ") name, goal1, goal2, score1 = Sort(name, goal1, goal2, score1) score1 = FirstRound(name, goal1, score1) print("\n ") print(" Second round : ") print(" ") name, goal2, goal1, score2 = Sort(name, goal2, goal1, score2) score2 = SecondRound(name, goal2, score2) print("\n ") print(" Final ranking : ") print(" ") name, scoreFinal = sortFinal(name, scoreFinal) scoreFinal = printFinal(name, scoreFinal) RE: How add 2 arrays? - nilamo - Jul-31-2017 Looking at the output, it appears as if it's correctly sorting the final scores. The issue is that it doesn't have the right values to sort them by. Whatever place someone was in in the last round, is what place they're treated as ALWAYS have been in. ie: in the last round, Henry was last, so he got 2 points, which puts him at 4 points overall since you're ignoring his original score. That said, I'm not sure how you got that output, since your code doesn't run for me. Line 46 is an IndexError... you're comparing two different values of scoreFinal, yet you've never assigned any values to scoreFinal, so any index would throw an indexerror. RE: How add 2 arrays? - Augustin1340 - Jul-31-2017 Thank you for your help. I am desperate since now two weeks... I try still and still but nothing. I have deleted the function "sortFinal" because it was false. I think ??? How must I to sort the final ranking ? Please ??? I don't have idea.... Is it my function printFinal() is correct ???? # --- Step 1/ Print names + points and goals def FirstRound(name, goal, score1): for i in range(len(name)): score1.append(point[i]) print("Name : " + name[i] + " \t " + str(score1[i]) + " points " + str(goal[i]) + " goals ") return score1 def SecondRound(name, goal, score2): for i in range(len(name)): score2.append(point[i]) print("Nom : " + name[i] + " \t " + str(score2[i]) + " points " + str(goal[i]) + " goals ") return score2 # --- Etape 2/ Sort points according goals def Sort(name, goal1, goal2, pts): swap = True while swap: swap = False for i in range(len(name) - 1): if goal1[i + 1] < goal1[i]: name[i], name[i + 1] = name[i + 1], name[i] goal1[i], goal1[i + 1] = goal1[i + 1], goal1[i] goal2[i], goal2[i + 1] = goal2[i + 1], goal2[i] swap = True return name, goal1, goal2, pts # --- Step 3/ Print the number of points def printFinal(name, scoreFinal): for i in range(len(name)): scoreFinal.append(str(score1[i] + score2[i])) print("Nom : " + name[i] + " \t " + (scoreFinal[i]) + " points " ) return scoreFinal print(scoreFinal) name = ["Henry", "Owen", "Drogba"] score1 = [] score2 = [] scoreFinal = [] goal1 = [10,8,6] goal2 = [4,6,5] point = [2,4,6] print(" First round : ") print(" ") name, goal1, goal2, score1 = Sort(name, goal1, goal2, score1) score1 = FirstRound(name, goal1, score1) print("\n ") print(" Second round : ") print(" ") name, goal2, goal1, score2 = Sort(name, goal2, goal1, score2) score2 = SecondRound(name, goal2, score2) print("\n ") print(" Final ranking : ") print(" ") name, goal2, goal1, scoreFinal = Sort(name, goal2, goal1, scoreFinal) scoreFinal = printFinal(name, scoreFinal) RE: How add 2 arrays? - nilamo - Aug-01-2017 If you change printFinal to look like this... def printFinal(name, scoreFinal): print(score1) print(score2) for i in range(len(name)): scoreFinal.append(str(score1[i] + score2[i])) print("Nom : " + name[i] + " \t " + (scoreFinal[i]) + " points ") return scoreFinal...you get this output... Why are score1 and score2 the same? You're getting the wrong output, because scoreFinal.append(str(score1[i] + score2[i])) is getting unrelated numbers. You're adding score1[i] and score2[i], but that doesn't make sense, because the indices of those lists are not related to who has that many points.
RE: How add 2 arrays? - Augustin1340 - Aug-01-2017 Hello, Thank you nilamo for your help. So, we know now the problem concerning my code. Do you have a solve to my problem or an example for help me? |