##### Assign the sum of 2 consecutive numbers in a list to a varibale
 Assign the sum of 2 consecutive numbers in a list to a varibale Fenaz Unladen Swallow Posts: 2 Threads: 1 Joined: Apr 2020 Reputation: Apr-04-2020, 08:35 PM ```import random no_questions = int(input("Enter the no of questions ")) questions = [] for i in range(0,no_questions*2): n = random.randrange(0,10) questions.append(n) def pairwise(iterable): a = iter(iterable) return zip(a, a) for x, y in pairwise(questions): input ("%d + %d " % (x, y))```The output will ask to enter the value of x + y, how can I assign the answer of each question to different variables Reply bowlofred Da Bishop Posts: 1,194 Threads: 3 Joined: Mar 2020 Reputation: Apr-04-2020, 10:05 PM One method is to append each answer to another list. ```answers = [] for x, y in pairwise(questions): answers.append(input ("%d + %d " % (x, y)))``` Reply Fenaz Unladen Swallow Posts: 2 Threads: 1 Joined: Apr 2020 Reputation: Apr-05-2020, 07:04 AM (This post was last modified: Apr-05-2020, 07:06 AM by Fenaz.) (Apr-04-2020, 10:05 PM)bowlofred Wrote: One method is to append each answer to another list. ```answers = [] for x, y in pairwise(questions): answers.append(input ("%d + %d " % (x, y)))``` It just created another list of answers but how do I assign the each answer to the questions ? Reply bowlofred Da Bishop Posts: 1,194 Threads: 3 Joined: Mar 2020 Reputation: Apr-05-2020, 10:30 PM (This post was last modified: Apr-05-2020, 10:30 PM by bowlofred.) You'd use math. For answers[x], the questions for it were questions[2 * x] and questions[2 * x + 1]. It might be easier to keep the parts of the questions in separate lists, then you could index all of them equally. Reply

 Possibly Related Threads… Thread Author Replies Views Last Post convert numbers into list lokesh 1 420 Jun-03-2021, 06:37 AM Last Post: menator01 adding numbers in a list Nickd12 2 580 Jan-15-2021, 12:46 PM Last Post: Serafim Print max numbers in a list jimmoriarty 1 543 Sep-25-2020, 07:29 AM Last Post: DPaul importing a list of numbers into python script barrypyth 8 1,287 Aug-22-2020, 09:10 PM Last Post: barrypyth Summing a list of numbers Oldman45 6 841 Jul-12-2020, 05:30 PM Last Post: Oldman45 How do I calculate a ratio from 2 numbers and return an equivalent list of about 1000 Pleiades 6 6,081 Jul-01-2020, 03:53 PM Last Post: ndc85430 Topic: “Filter numbers with a list comprehension” (PyBite #107) Drone4four 4 846 Jun-11-2020, 08:31 PM Last Post: Drone4four Python Consecutive characters vijju56 4 1,407 Apr-22-2020, 11:03 AM Last Post: TomToad creating consecutive rows PyPy 1 614 Feb-07-2020, 06:46 PM Last Post: micseydel output a list of random numbers 'x' columns wide adityavpratap 4 1,037 Jan-13-2020, 05:32 PM Last Post: perfringo

Forum Jump:

### User Panel Messages

##### Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020