0

I have a program that asks a user to come up with a question and then to type the answer. Right now I have it asking the user to type in 5 questions and answers. So basically it asks the user for a question and its answer 5 times. What I would really like it to do is ask the user for how many questions they would like to type and then based on that it presents the user with "type your question" and "type your answer" and stores those things as variables (i.e. "q1" and "a1" and repeating based on how many questions/answers they want to type) so I can then use these variables in a print statement later on in the program. I was thinking about using a while loop with a continue condition until and count down to 0 and then the loop ends but how do I constantly create new variables?

`   oneistart= raw_input('What is the first question: ')
oneiend= raw_input('What is the first answer: ')
user3527972
  • 89
  • 2
  • 7
  • 2
    You need a dictionary to do this. Your question is the `key` and answer is the relevant 'value'. You should research on them. – ha9u63a7 Mar 21 '15 at 12:28
  • 1
    You really _don't_ want to create multiple variables. :) As ha9u63ar said, a `dict` is good for this. See [How do I do variable variables in Python?](http://stackoverflow.com/questions/1373164/how-do-i-do-variable-variables-in-python) and [How can I create lists from a list of strings?](http://stackoverflow.com/questions/14241133/how-can-i-create-lists-from-a-list-of-strings) – PM 2Ring Mar 21 '15 at 12:31
  • 2
    Using a `dict` lets you find the answer if you're given the question. Another data structure worth considering for this is a list of tuples, with the question as one member of the tuple and the answer as the other. – PM 2Ring Mar 21 '15 at 12:39

2 Answers2

1

How about working with a dictionary?

d = dict()  # Creates an empty dictionary

oneistart = raw_input('What is the first question: ')
oneiend = raw_input('What is the first answer: ')

d[oneistart] = oneiend  # updates the dictionary with new key-value (or updates existing value associated to a key

Also, if you call d.values() you will get a list of all your values (i.e. answers) and you can see how many answers you have got? There are also other functions associated to a dictionary object, which you can research on by reading the documentation (your task!).

Is this what you had in mind?

Eric O. Lebigot
  • 91,433
  • 48
  • 218
  • 260
ha9u63a7
  • 6,233
  • 16
  • 73
  • 108
  • Your `d[oneistart] = oneiend # updates the dictionary with new key-value` isn't inside the code block. Also, those semicolons aren't necessary. – PM 2Ring Mar 21 '15 at 12:36
  • @PM2Ring Semicolon is a good practice - not about necessity. I work with C/C++/Java at the same time, so doing it has become a habit - not a dealbreaker I hope :) – ha9u63a7 Mar 21 '15 at 12:39
  • Semicolons are arguably *not* good Python practice: virtually nobody uses them, in Python, so your code looks strange, as if it was written by someone who does not know about Python practices, so this can send the wrong signal. – Eric O. Lebigot Mar 22 '15 at 02:40
0

I think, better than create dynamic variables in python, save it as list of dicts like.

lis = []
n = int(raw_input("How many? "))
for i in xrange(n):
    q = raw_input("enter q: ")
    a = raw_input("enter a: ")
    lis.append({"q"+str(i+1): q, "a"+str(i+1): a})
print lis
>>>[{'q1': 'ques1', 'a1': 'ans1'}, {'q2': 'ques2', 'a2': 'ans2'}]
itzMEonTV
  • 19,851
  • 4
  • 39
  • 49