1

Can somebody explain to me why in the following code I get 'your answer is None' please?

question='should save the notebook after edit?(T/F) :'
correct_ans=('t')

def tf_quiz(question,correct_ans):
    if input(question)==correct_ans:
        print('correct')
    else:
        print('incorrect') 

quiz=tf_quiz(question,correct_ans)

print('your answer is',quiz)

output:

should save the notebook after edit?(T/F) :t
correct
your answer is None

3 Answers 3

2

Your function doesn't explicitly return anything, so implicitly it returns None. Instead of printing inside the function, just return the value you want:

def tf_quiz(question,correct_ans):
    if input(question) == correct_ans:
        return 'correct' # Here
    else:
        return 'incorrect' # And here
Sign up to request clarification or add additional context in comments.

Comments

1

Because tf_quiz function does not return a value, it only prints it. Use return 'correct' and so on.

Comments

1

Your function "tf_quiz" does not return any output. By default python returns None as the return value of a function, unless it is specified to return something else. So this is how you should correct your code.

question='should save the notebook after edit?(T/F) :'
correct_ans='t'

def tf_quiz(question,correct_ans):
    usr_answer = input(question) 
    if usr_answer==correct_ans:
        print('correct')
    else:
        print('incorrect') 
    return usr_answer

quiz=tf_quiz(question,correct_ans)

print('your answer is',quiz)

Comments

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.