Python - still new, can't figure this out


#1

I've been going through the Python exercises on here and for the most part I actually 'get' it but while trying to do a very simple Magic 8-ball script I've run into an issue that I saw in exercises and never figured out. Code is below.

import random
print ("Please enter a yes or no question and hit Return: ")
question = input(" ")
answer = random.randint(0,2)
def reply(a):
    if answer == 0:
        print ("Yep")
    elif answer == 1:
        print ("Mebbe")
    elif answer == 2:
        print ("Nope")
    #return (a)
print ("You asked:",question)
print ("The answer is: ",reply)

The issue is probably obvious but I am oblivious to it. What is wrong with my function? Why does it not display an answer? I realise I haven't called the function in this bit as I've tried a few ways like return and print (response) and clearly I don't know how to. I can get it to work without a function but part of my reason for doing this was to figure out how to use a function for this. Be gentle.


#2

calling a function requires parentheses:

reply(answer)

between the parentheses supply arguments to satisfy function parameters


#3

All that does is return the number, not the answer. Instead of, for example, "The answer is: Yep" it gives me "The answer is: 0".


#4

Did you fix your function call? can i see an updated version of your code


#5

This is returning the Yep, Nope, Mebbe but also a secondary 0, 1, 2 that is not the one from the original reply. I get, for example, "Yep" then underneath "The answer is: 1" which isn't correct so it seems to be calling "answer" two separate times instead of once.

print ("Please enter a yes or no question and hit Return: ")
question = input(" ")
answer = random.randint(0,2)
def reply(a):
    answer = random.randint(0,2)
    if answer == 0:
        print ("Yep")
    elif answer == 1:
        print ("Mebbe")
    elif answer == 2:
        print ("Nope")
    return (a)
print ("You asked:",question)
print ("The answer is: ",reply(answer))

#6

nope, nothing is returned? You only print

i ran your code on repl, and it works fine? Where are you running the code?


#7

Sorry I should clarify that the "Yep", "Nope", and "Mebbe" are my code's replies. I am running it through IDLE Python 3.6


#8

Difference between printing something and returning something. Make sure you know that difference.


#9

Can i see a screenshot of you run this code in your IDLE?


#10

Returning gives it back to the function, printing puts it in the console. I think?

@stetim94


#11

you should return the answer (yep, mebbe, nope)

return means the function hands something back, so using print() on the function call will print what the function returns


#12

Ah I see it. Needed to return (answer) instead of return(a) OK this code works.

import random
print ("Please enter a yes or no question and hit Return: ")
question = input(" ")
answer = random.randint(0,2)
def reply(a):
    if answer == 0:
        print ("Yep")
    elif answer == 1:
        print ("Mebbe")
    elif answer == 2:
        print ("Nope")
    return (answer)
print ("You asked:",question)
print ("The answer is: ",reply(answer))

The function prints as expected and I get the number printed too (Yep, answer is 0). How do I get it to print "the answer is: " and then add the actual response instead of the random number? I realise I could just change each to "The answer is Mebbe", "The answer is Yep" but I want to know how to do it so it prints the string, not the integer.


#13

got this covered already?

your function should return what you want printed.

because your function call is printed, what the function returns will be printed


#14

print ("The answer is: "),reply(answer)
This gives me
The answer is:
Yep, Nope, Mebbe

print ("The answer is: ",reply(answer))
This gives me
Yep, Nope, Mebbe
The answer is: 0, 1, 2

I know you're trying to let me figure it out but I do not see how to get what I want here which is
The answer is: Yep, Nope, Mebbe


#15

simply do:

print ("The answer is: ",reply(answer))

then return Yep, Nope and Mebbe. Because your function call is now part of print, what the reply function returns will be printed on the same line


#16

import random

def reply(a):
    if a == 0:
        print ("Yep")
    elif a == 1:
        print ("Mebbe")
    elif a == 2:
        print ("Nope")

answer = random.randint(0, 2)

print(reply(answer))

#17

Try This :slight_smile:

import random
print ("Please enter a yes or no question and hit Return: ")
question = input(" ")
answer = random.randint(0,2)
def reply(a):
if answer == "0":
print ("Yep")
elif answer == "1":
print ("Mebbe")
elif answer == "2":
print ("Nope")
#return (a)
print ("You asked:",question)
print ("The answer is: ",reply)


#18

i think the solution should be:

import random
print ("Please enter a yes or no question and hit Return: ")
question = input(" ")
answer = random.randint(0,2)
def reply(a):
    if answer == 0:
        return "Yep"
    elif answer == 1:
        return "Mebbe"
    elif answer == 2:
        return "Nope"
print ("You asked:",question)
print ("The answer is: ",reply(answer))

this way, the function returns the possible answers, which are then printed (because reply function call is inside a print function call)

the function parameter of reply will become reduntant, so we can remove it.


#19

Ahhhhhh! I see what you were trying to hint at now. Instead of returning the integer "0, 1, 2" and printing "Yep, Nope, Mebbe" I had to simply return "Yep, Nope, Mebbe" to the function. Thank you @stetim94 that was driving me crazy.


#20

i was trying to hint you that 3 answers ago :wink: