Assign the sum of 2 consecutive numbers in a list to a varibale - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Assign the sum of 2 consecutive numbers in a list to a varibale (/thread-25598.html) |
Assign the sum of 2 consecutive numbers in a list to a varibale - Fenaz - Apr-04-2020 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 RE: Assign the sum of 2 consecutive numbers in a list to a varibale - bowlofred - Apr-04-2020 One method is to append each answer to another list. answers = [] for x, y in pairwise(questions): answers.append(input ("%d + %d " % (x, y))) RE: Assign the sum of 2 consecutive numbers in a list to a varibale - Fenaz - Apr-05-2020 (Apr-04-2020, 10:05 PM)bowlofred Wrote: One method is to append each answer to another list. It just created another list of answers but how do I assign the each answer to the questions ? RE: Assign the sum of 2 consecutive numbers in a list to a varibale - bowlofred - Apr-05-2020 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. |