Python Forum

Full Version: Assign the sum of 2 consecutive numbers in a list to a varibale
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
One method is to append each answer to another list.

answers = []
for x, y in pairwise(questions):
   answers.append(input ("%d + %d " % (x, y)))
(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 ?
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.