0

I'm moving through LPTHW and I am trying to create a game where you have to answer a series of questions correctly to move to another room. If you answer incorrectly you're sent back or the game ends.

How does one update a variable from, in this case, ans, to reflect a new value? My answer seems stuck on the first correct answer value.

I converted the dict of question/ answer pairs to a list of tuples. When a question is used, an empty list appends that question and answer tuple and the original list removes that same tuple so as not to be used again. Any guidance is greatly appreciated, thanks.

http://pastebin.com/5NAaDv82

Sample output from powershell: where is zion national park?

utah #raw input

utah utah # this is output from print statement

That's Correct!

You have 2 questions to go Correct Answers: 1

[('where is zion national park?', 'utah')] #this is the empty list updated with q/a key/value

what was the shop cats name?

furball

furball # still compared to first answer utah??

Wrong Answer 2 [('where is zion national park?', 'utah'), ('what was the shop cats name?', 'furball')]

what is 'Rauh Welt'?' rough world

>

2
  • You'll need a real problem statement for us to help you.
    – Eric Urban
    Aug 1, 2013 at 23:57
  • Hi Eric, my problem is trying to construct the right question to ask. I guess the question should be, if I use .items on a dictionary to create a list of tuples, when I pull a value from a particular tuple, after testing, how do I remove that stored value and advance to the next selection? I hope that's more clearly stated.
    – Kevin T.
    Aug 2, 2013 at 12:14

1 Answer 1

0

Each time you use raw_input() python will block and wait for input from the user. Once the user provides that input (and presses enter) it will then return the value the user has entered.

Each subsequent time you wish to query the user for information, you must use raw_input again to get that information :

ans1=raw_input('Enter the answer to question 1') ans2=raw_input('Enter the answer to question 2')

Every time you want a new answer, you'll need to use raw_input to get it.

Note that at the end of your script you are doing this:

for i in keys:  #next 3 line compare used question to keys and if matched
   if i in qlist: # ...removes tuple from keys
       keys.remove(i)
       new_question = keys[0][0] # selects new question
       print new_question
       global ans
       ans = keys[0][1]
       print ans
       new_ans = raw_input("> ")
       ans = new_ans

Here you loop through keys, and if the key is in qlist you remove it and ask for a new answer. However, in the case where the original answer was wrong (and i is not in qlist) you just return back to the while loop.

It looks like you should examine your indenting, as you've probably got code in a block that doesn't belong there.

2
  • Ok, but the input is updated in the loop and is being tested against the new value. What seems to be happening is that my value in the tuple is stuck and therefore not clearing out for the next question/ answer.
    – Kevin T.
    Aug 2, 2013 at 0:28
  • Ok, I think I get it. For my indenting, I still get confused with where I need to back out of a code block. Thanks Big C, I appreciate the help.
    – Kevin T.
    Aug 2, 2013 at 0:59

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.