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: ')
3
  • 2
    You need a dictionary to do this. Your question is the key and answer is the relevant 'value'. You should research on them. Commented Mar 21, 2015 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? and How can I create lists from a list of strings? Commented Mar 21, 2015 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. Commented Mar 21, 2015 at 12:39

2 Answers 2

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?

Sign up to request clarification or add additional context in comments.

3 Comments

Your d[oneistart] = oneiend # updates the dictionary with new key-value isn't inside the code block. Also, those semicolons aren't necessary.
@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 :)
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.
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'}]

5 Comments

You can create variables dynamically in Python. One of of doing it is globals()["q1"] = ….
for loops are meant to do what you emulate with your while loop.
I removed the i = 0, and the i += 1.
@EOL but it gives starts from q0 . So one more edit :)
You're right. Yeah, this or for i in range(1, xrange(n+1)).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.