0

I'm asking the user to input a answer, but I also want the question to be asked again if they fail to get the correct answer. This is my attempt but I am thinking I'm going to need a loop? New to python so not fully sure how to go about this. If they answer correctly, my code executes the secondquestion(). Thanks to all that help.

def firstquestion(): #first question
    firstquestion = int(input('What is the square root of 9? '))
    if firstquestion == 3:
        print('Correct!, next question!')
        secondquestion()
    else:
        print('Wrong!, Think harder!')
    firstquestion()
Weafs.py
  • 22,731
  • 9
  • 56
  • 78

3 Answers3

4

In Python, the following

def test():
    test = 1
    test()

turns test into a variable with value 1 rather than your original function. So trying to call it creates an error.

In any case, when you want to execute a series of functions sequentially it is better to use an external function that calls them one after the other, rather than calling them from within one another. This can get messy and cause stack overflow. The name of the function firstquestion also suggests that it should simply ask the first question - not ask the first question, then the second, then the third and so on.

So it may be easier to put the loop outside of your question functions, and use a return value to indicate whether it was answered correctly or not.

def firstquestion():
    answer = int(input('What is the square root of 9? '))
    if answer == 3:
        return True

def secondquestion():
    answer = int(input('What is 4 + 5? '))
    if answer == 9:
        return True

for question in [firstquestion, secondquestion]:
    while question() is not True:
        print('Wrong, think harder!')
    print('Correct, next question!')
Stuart
  • 9,597
  • 1
  • 21
  • 30
2

Because your pattern seems to involve a question (questionText) and an answer (answer), perhaps it would be better to create a function simply for that purpose (question). This removes code duplication and makes it easier to extend in the future.

In this example I created a function called question that accepts parameters questionText and answer. Then I created a list of questions (composed of a questionText and an answer) that I would like to ask, in the order that they should be asked.

Then I use a for loop to iterate through all questions, assigning questionText and answer in each iteration. There is a while loop inside this for loop that continues to ask the user for a response. If the response is correct, the question will return True, and the while loop will stop because question(questionText, answer) is True (which is opposite of the while loop's condition). Then after a correct response, Correct! Next question! is printed, and it moves to the next question from the list.

# Defines a 'question' function which accepts:
#     questionText - the question to be asked
#     answer - the answer to the question
# Returns True if the answer is correct
def question(questionText, answer):
    if int(input(questionText + ' ')) == answer:
        return True

# Creates a list of questions and their respective answers
questionList = [
        ('What is the square root of 9?', 3),
        ('What is 2 plus 2?', 4),
        ('What is 10 divided by 5?', 2)
    ]

# Iterates through all questions in the questionList and asks the user each question.
# If the user response equals the answer, outputs 'Correct! Next question!'
# If the user response does not equal the answer, outputs 'Wrong! Think harder!'
for questionText, answer in questionList:
    while question(questionText, answer) is not True:
        print('Wrong! Think harder!')
    print('Correct! Next question!')
grovesNL
  • 6,016
  • 2
  • 20
  • 32
  • I like the idea of generalizing the code to DRY. But in this case, making the assumption that it is always possible to accept an answer using the == operator is not very flexible. You could, for example, want to check the answer of some questions against an RegEx for example. – Governa Nov 16 '14 at 23:44
  • @Governa: Sure, I would also suggest that `int` isn't necessarily the best data type for a math questionnaire. However for the simple case that was given, this should be fine. Of course it's easily extended for future cases. – grovesNL Nov 16 '14 at 23:58
-1
while True:
    answer = int(input('What is the square root of 9? '))    
    if answer == 3:
        print('Correct!, next question!')
        break
    else:
        print('Wrong!, Think harder!')

This is an example of what is known as tail recursion. You should check for type errors too (e.g. what happens when the user types "frog" as the answer?).

demented hedgehog
  • 7,007
  • 4
  • 42
  • 49