为了账号安全,请及时绑定邮箱和手机立即绑定

python函数输出无

python函数输出无

四季花海 2021-09-28 15:18:09
import randomdef 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)
查看完整描述

2 回答

?
一只甜甜圈

TA贡献1836条经验 获得超5个赞

您传入随机数 asanswerNumber然后检查 的值getAnswer。因此,该函数试图将一个函数的引用与一个整数进行比较,该整数显然永远不会计算为True。因为没有一个if语句计算为True,所以它会到达函数的末尾而不会遇到 return 语句,因此默认情况下返回None。


将getAnswer函数体中的所有引用替换为answerNumber,您应该都设置好了。就像是:


import random

def 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)


查看完整回答
反对 回复 2021-09-28
?
慕的地10843

TA贡献1785条经验 获得超8个赞

import random

def 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与代码中的数字进行比较。


查看完整回答
反对 回复 2021-09-28
  • 2 回答
  • 0 关注
  • 217 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信