Python 函数返回输出';无';
有人能给我解释一下为什么在下面的代码中我会得到“你的答案是无”吗Python 函数返回输出';无';,python,python-3.x,Python,Python 3.x,有人能给我解释一下为什么在下面的代码中我会得到“你的答案是无”吗 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
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)
输出:
should save the notebook after edit?(T/F) :t
correct
your answer is None
函数没有显式返回任何内容,因此隐式返回
None
。不在函数内部打印,只需返回所需的值:
def tf_quiz(question,correct_ans):
if input(question) == correct_ans:
return 'correct' # Here
else:
return 'incorrect' # And here
函数没有显式返回任何内容,因此隐式返回
None
。不在函数内部打印,只需返回所需的值:
def tf_quiz(question,correct_ans):
if input(question) == correct_ans:
return 'correct' # Here
else:
return 'incorrect' # And here
因为tf_quick函数不返回值,所以它只打印它。使用
返回“correct”
等等。因为tf_quick函数不返回值,它只打印它。使用返回“correct”
等等。您的函数“tf\u quick”不会返回任何输出。默认情况下,python返回None作为函数的返回值,除非指定它返回其他值。
因此,这就是您应该如何更正代码的方法
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)
函数“tf_quick”不返回任何输出。默认情况下,python返回None作为函数的返回值,除非指定它返回其他值。
因此,这就是您应该如何更正代码的方法
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)