0
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)
2
  • 1
    Add "print(answerNumber)" to the end of getAnswer, that will tell you what you've passed in. But really you need to check the code, you are checking getAnswer when I believe you want answerNumber in the if logic. Commented Dec 28, 2018 at 22:16
  • I didn't realize i was typing getAnswer in the if logic thank you! Commented Dec 28, 2018 at 22:22

2 Answers 2

5

You're passing in the random number as answerNumber and then checking against the value of getAnswer. So, the function is trying to compare a reference to a function with an integer, which will obviously never evaluate to True. Because none of the if statements evaluate to True, it gets to the end of your function without hitting a return statement, and so by default returns None.

Replace all of the getAnswer references in the function body with answerNumber and you should be all set. Something like:

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)
Sign up to request clarification or add additional context in comments.

Comments

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

you were comparing the function getAnswer to the integer value and since no else statement defined there, function call return None.

all i changed was to compare the actual number answerNumber to the number in code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.