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)
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与代码中的数字进行比较。
添加回答
举报