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
|
How add 2 arrays? - Augustin1340 - Jul-26-2017 Hello all, I am a student who learns Python. I should in my exercise create several functions (only) and manipulate several arrays. 1) first step: Create a function whose enters 3 soccer player (names) and whose enters the number of goals also [OK] Example: Thierry Henry => 20 goals Lionel Messi => 32 goals Karim Benzema => 16 goals 2) second step: Create a function whose sorts goals from smallest to biggest [OK] 3) third step: Create a function whose prints the name of the soccer player and the number of goals. [OK] Example: Karim Benzema => 16 goals Thierry Henry => 20 goals Lionel Messi => 32 goals 4) fourth step: Create a function whose attributes scores from smallest to biggest. [OK] Example: Karim Benzema has 2 scores Thierry Henry has 4 scores Lionel Messi has 6 scores 5) fifth step: Create a function whose enters still goals (second rounds) with always the same soccer player. [OK] Example Karim Benzema => 10 goals Thierry Henry => 8 goals Lionel Messi => 14 goals 6) Sixth step: Create a function whose sorts still goals. [OK] 7) Seventh step: Create a function whose prints the name of the soccer player and the number of goals (first round and second rounds) . [OK] 8)Eighth step: Create a function whose attributes still scores from smallest to biggest (second round) [OK] Example: Thierry Henry has 2 scores Karim Benzema has 4 scores Lionel Messi has 6 scores 9) Ninth step: I would like to create a function whose will add the array score1 with the array score2. Here is the result: Karim Benzema 4 scores Thierry Henry 6 scores Lionel Messi 12 scores I don’t should to use of dictionary !!! Thank you a lot for your help… name = ; goal = ; point = [2,4,6] score = goal2 = score2 = result = 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(name, score, score2, result): for i in range( len( name) ): result.append(name[i] + " " + str(score[i]+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(name, score, score2, result)[/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i] RE: How add 2 arrays? - micseydel - Jul-26-2017 You should break down the problem(s) you're having. Any question should typically revolve around 5-10 lines of code, posting your assignment and 100 lines is not optimal for attracting answers. RE: How add 2 arrays? - Augustin1340 - Jul-26-2017 Hello, Sorry ... To begin I would like to reduce my functions. For example; The function printPoint1 and printPoint2 are the sames functions. How add the array score with the array score2 ??? 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 printPoint2(name, point, score2): for i in range(len(name)): print("Name : " + name[i] + " Your score is of " + str(point[i]) + " : " + str(goal[i]) + " goals. ") score2.append(point[i]) RE: How add 2 arrays? - sparkz_alot - Jul-26-2017 Add them together: >>> a = [1, 2, 3, 4] >>> b = [5, 6, 7, 8] >>> c = a + b >>> c [1, 2, 3, 4, 5, 6, 7, 8] >>> RE: How add 2 arrays? - nilamo - Jul-26-2017 Quote:How add the array score with the array score2 ??? That depends, what does "add" mean here? Does it mean having an output that's len(score1)+len(score2) elements long? Or does it mean having output that's len(score1) elements long, where each element is calculated from the two constituent lists, like score1[index] + score2[index] ?
RE: How add 2 arrays? - Augustin1340 - Jul-26-2017 (Jul-26-2017, 06:01 PM)nilamo Wrote:Quote:How add the array score with the array score2 ??? Yes, I want to calculate the array score with the array score2 name = []; goal = []; point = [2,4,6] score = [] goal2 = [] score2 = [] result = [] 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 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 printScore(name, point, score, score2): result = []; for i in range(len(name)): result.append(name[i] + " Your score is of " + str(point[i]) + str(score[i] + score2[i])); print(result) name, goal = demand(3) tri1(goal, name) name, goal, goal2 = demand2(3) Tris2(goal2, name, goal) printScore(name, point, score, score2) RE: How add 2 arrays? - nilamo - Jul-26-2017 Isn't that what you're already doing? How is what you have different from what you want? RE: How add 2 arrays? - Augustin1340 - Jul-27-2017 Do you can to delete my post please here is a topic better https://python-forum.io/Thread-Arrays-and-functions Regards Arrays and functions - Augustin1340 - Jul-27-2017 Hello; Sorry for my english…. I would like to know my errors please. Here are instructions. The function demand() allows to enter names and goals… (first round) Example: Thierry Henry 14 goals Karim Benzema 16 goals David Trezeguet 18 goals The function printTri1 allows to print the names and goals. The function printPoint1(name, point, score): allows to attribute points. Example Thierry Henry has 2 points Karim Benzema 4 points David Trezeguet 6 points The function demand2() allows to enter names and goals… (second round) Example: Thierry Henry 6 goals Karim Benzema 8 goals David Trezeguet 10 goals The function printPoint2(name, point, score2): allows to attribute points. Example Thierry Henry has 2 points Karim Benzema 4 points David Trezeguet 6 points I don’t know if it’s clear ? Now I want to create alone function to calculate points and delete the function printPoint1 and printPoint2. Here is the result that I wish with the function printScore. Thierry Henry 4 points Karim Benzema 8 points David Trezeguet 12 points Thank you very much. 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 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 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(name, score, score2, result): for i in range( len( name) ): result.append(name[i] + " " + str(score[i]+score2[i])); name, goal = demand(3); printTri1(name, goal) printPoint1(name, point, score) name, goal, goal2 = demand2(3) printPoint2(name, point, score2) printScore(name, score, score2, result)[/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i] RE: How add 2 arrays? - sparkz_alot - Jul-27-2017 Quote: I would like to know my errors please. Here are instructions. Please don't post your entire homework. Few are going to read it. You would do better to post an example of your output versus what you expect your output to look like. As to the errors, what errors do you get when you run your code. Post the error code, in it's entirety, between the 'error' code tags. |