Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I'm trying to get my program to iterate through a loop after a person gets the correct answers to the first question. I feel like there is a better way to do things syntactically.

math_questions = [
    {'question1':'1*1',
    'answer1':1,
    'quote1' :'What you are,you are by accident of birth; what I am,I am by myself.\n There are and will be a thousand princes; there is only one Beethoven.'},
    {'question2':'2*1',
    'answer2':2,
    'quote2': 'Two is company, three is a crowd'},
    {'question3': '3*1',
    'answer3': 3,
    'quote3': 'There are three types of people, those who can count and those who cannot'}
    ]

# read from a txt file later???

for question in math_questions:
    print math_questions[0]['question1']
    math_answer = int(raw_input("What is the answer to " + math_questions[0]["question1"] +"? : "))
    if math_answer == math_questions[0]['answer1']:
        print math_questions[0]['quote1']
    else:
        print "Try again"
    print math_questions[0]['answer1'] 
share|improve this question

closed as off-topic by David Harkness, syb0rg, rolfl Jun 1 at 4:32

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions containing broken code or asking for advice about code not yet written are off-topic, as the code is not ready for review. Such questions may be suitable for Stack Overflow or Programmers. After the question has been edited to contain working code, we will consider reopening it." – David Harkness, syb0rg, rolfl
If this question can be reworded to fit the rules in the help center, please edit the question.

    
This code has obvious problems. You have a loop, on question, but that is never used. Instead, you have hard-coded references to math_questions[0]. This code does not ask three quesitions, but one question three times. –  rolfl Jun 1 at 4:32
add comment

1 Answer

In your situation, I would recommend using a named tuple:

import collections

MathQuestion = collections.namedtuple('MathQuestion', ['question', 'answer', 'quote'])


math_questions = [
    MathQuestion(question='1*1', answer=1, 
        quote='What you are,you are by accident of birth; what I am,I am by myself.\n There are and will be a thousand princes; there is only one Beethoven.'),
    MathQuestion(question='2*1', answer=2,
        quote='Two is company, three is a crowd'),
    MathQuestion(question='3*1', answer=3,
        quote='There are three types of people, those who can count and those who cannot')
    ]

# read from a txt file later???

for question in math_questions:
    correct_answer = False
    while not correct_answer:
        math_answer = int(raw_input("What is the answer to " + question.question +"? : "))
        if math_answer == question.answer:
            correct_answer = True
            print question.quote
        else:
            print "Try again"

Note other changes:

In your original code, you would go on to the next question even if the person answered incorrectly. I added a while loop to support that functionality. Second, you were looping through the questions, you had hard coded the index within math_questions, so you never display anything of the other questions. As the for loop loops through items within math_question, question will always refer to the current item, so you don't need any index offsets.

share|improve this answer
add comment

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