Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Assign the sum of 2 consecutive numbers in a list to a varibale
#1
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
Quote
#2
One method is to append each answer to another list.

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

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How do I calculate a ratio from 2 numbers and return an equivalent list of about 1000 Pleiades 7 1,499 Jul-01-2020, 03:53 PM
Last Post: ndc85430
  Topic: “Filter numbers with a list comprehension” (PyBite #107) Drone4four 4 174 Jun-11-2020, 08:31 PM
Last Post: Drone4four
  Python Consecutive characters vijju56 4 228 Apr-22-2020, 11:03 AM
Last Post: TomToad
  creating consecutive rows PyPy 1 247 Feb-07-2020, 06:46 PM
Last Post: micseydel
  output a list of random numbers 'x' columns wide adityavpratap 4 262 Jan-13-2020, 05:32 PM
Last Post: perfringo
  Randomly assign values in List 1 to a value in List 2 Seneca260 7 856 Dec-16-2019, 12:57 PM
Last Post: BamBi25
  How do I name a list while generating numbers on the fly? Pleiades 6 438 Dec-03-2019, 05:47 PM
Last Post: jefsummers
  Subtract 11 from entire list of quoted numbers Pleiades 1 271 Nov-14-2019, 10:26 AM
Last Post: Larz60+
  List of numbers to add Planeuge 4 418 Sep-21-2019, 10:41 PM
Last Post: Planeuge
  weird result trying to remove numbers from a list Exsul 6 521 Aug-27-2019, 05:10 AM
Last Post: perfringo

Forum Jump:


Users browsing this thread: 1 Guest(s)