python函数输出无

import random

def getAnswer (answerNumber):

    if getAnswer == 1:

        return 'It is certain'

    elif getAnswer == 2:

        return 'It is decidedly so'  

    elif getAnswer == 3:

        return 'Yes' 

    elif getAnswer == 4:

        return "Reply hazy try again"

    elif getAnswer == 5:

        return 'Ask again later'

    elif getAnswer == 6:

        return "Concentrate and ask again"

    elif getAnswer == 7:

        return 'My reply is no'

    elif getAnswer == 8:

        return 'not so good'

    elif getAnswer == 9:

        return 'doubtful'


r = random.randint(1 , 9)

future = getAnswer(r)

print(future)


四季花海
浏览 218回答 2
2回答

一只甜甜圈

您传入随机数 asanswerNumber然后检查 的值getAnswer。因此,该函数试图将一个函数的引用与一个整数进行比较,该整数显然永远不会计算为True。因为没有一个if语句计算为True,所以它会到达函数的末尾而不会遇到 return 语句,因此默认情况下返回None。将getAnswer函数体中的所有引用替换为answerNumber,您应该都设置好了。就像是:import randomdef getAnswer(answerNumber):    if answerNumber == 1:        return 'It is certain'    elif answerNumber == 2:        return 'It is decidedly so'      elif answerNumber == 3:        return 'Yes'     elif answerNumber == 4:        return "Reply hazy try again"    elif answerNumber == 5:        return 'Ask again later'    elif answerNumber == 6:        return "Concentrate and ask again"    elif answerNumber == 7:        return 'My reply is no'    elif answerNumber == 8:        return 'not so good'    elif answerNumber == 9:        return 'doubtful'r = random.randint(1 , 9)future = getAnswer(r)print(future)

慕的地10843

import randomdef getAnswer (answerNumber):    if answerNumber == 1:        return 'It is certain'    elif answerNumber == 2:        return 'It is decidedly so'      elif answerNumber == 3:        return 'Yes'     elif answerNumber == 4:        return "Reply hazy try again"    elif answerNumber == 5:        return 'Ask again later'    elif answerNumber == 6:        return "Concentrate and ask again"    elif answerNumber == 7:        return 'My reply is no'    elif answerNumber == 8:        return 'not so good'    elif answerNumber == 9:        return 'doubtful'r = random.randint(1 , 9)future = getAnswer(r)print(future)您正在将函数getAnswer与整数值进行比较,并且由于else那里没有定义语句,函数调用 return None。我所做的只是将实际数字answerNumber与代码中的数字进行比较。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python