Tom
Tom

Reputation: 71

True False Quiz Function in Python

I have created the following program in Python:

def tf_quiz(question, correct_ans):
    if input(question) == correct_ans:
        return("correct")
    else:
        return("incorrect")

quiz_eval = tf_quiz("Birds can fly", "T")

print("your answer is", quiz_eval)

However, my output is as following:

Birds can flyF
your answer is incorrect

But I want the following output:

(T/F) Birds can fly: F
your answer is incorrect

What do I need to change in my code?

Upvotes: 0

Views: 5090

Answers (3)

Samwise
Samwise

Reputation: 71454

Change:

if input(question) == correct_ans:

to:

if input(f"(T/F) {question}: ") == correct_ans

if you want the caller to be able to specify each question without the extra formatting pieces.

Upvotes: 1

John Gordon
John Gordon

Reputation: 33335

Either update the question text to have the exact formatting you want:

quiz_eval = tf_quiz("(T/F) Birds can fly: ", "T")

Or update the input() call to insert generic formatting for a true/false question:

if input("(T/F) %s: " % question) == correct_ans:

Upvotes: 1

Green-Avocado
Green-Avocado

Reputation: 891

Your question is missing a "(T/F) " string at the beginning, and a colon and space at the end.

def tf_quiz(question, correct_ans):
    if input("(T/F) " + question + ": ") == correct_ans:
        return("correct")
    else:
        return("incorrect")

quiz_eval = tf_quiz("Birds can fly", "T")

print("your answer is", quiz_eval)

Upvotes: 1

Related Questions