Simple Question - Store the results of functions in variables


#1

I’m an absolute beginner but this simple part of my cointoss program has been driving me a bit mad. Whenever I input ‘heads’, it still returns 0 outside the function… what am I doing wrong? I also acknowledge there are premade functions that can probably do all of this much more efficiently. I’m just trying to get my hands dirty so to speak. See code below, thanks! :smiley:

def coinpick(playerpick):
    coinchoice = input ("...and up it goes! Heads or Tails?\n").lower()
    if coinchoice == "heads":
        print ("\nOk")
        playerpick = 1
        print (playerpick)
        return playerpick
    elif coinchoice == "tails":
        print ("\nAlright")
        playerpick = 0
        print (playerpick)
        return playerpick
    else:
        print ("\nWay to mess up a simple cointoss. Let's try this again...")
        coinpick()

coinpick(playerpick)

test = input("Playerpick was " + str(playerpick))
print (test)

#2

You ask for input after calling your function. You’d need time travel capabilities to make it have an effect on how your function runs.
You’re also using variables that have not been defined.

Oh, you ask for input both inside and outside the function …Why?

If I start removing things that don’t do anything, or does things it shouldn’t, I end up with…

def coinpick(coinchoice):
    coinchoice = coinchoice.lower()
    if coinchoice == "heads":
        return 1
    elif coinchoice == "tails":
        return 0
    raise ValueError("expected heads or tails, got " + str(coinchoice))

(Added the exception at the end, there’s no sensible response so it should raise an exception if that happens)

Your function is both taking input and classifying it, that sounds to me like the function has too many jobs. Or if it’s just supposed to do “everything”, then there shouldn’t be any code outside the function.

Your function was also ignoring the argument given to it, that’s kind of like if the + operator ignored the values given to it and always returned the same number!

Also, not quite sure what 0 and 1 have to do with it, the strings might be fine just how they are!

…Well. Oops. I guess I end up stripping everything away. But at least I did touch on some things that are definitely bugs - using undefined variables, your function having a parameter it doesn’t use, taking input in multiple places


#3

Hi ionatan - thanks for responding! I think the input you referred to as being called outside my function was this if I’m not mistaken:

test = input("Playerpick was " + str(playerpick))
print (test)

However, I just meant to put that piece of code in as test in order to see whether the playerpick would store outside for the next function. The next function would be the actual flip and would see whether the playerpick matched the actual result of the cointoss and who consequently goes first. Now that I’ve written it out, I see that perhaps if I were to break it up into multiple functions, it may be easier…

Also see your point on not changing the strings to integers for no good reason as well. Thanks for the thorough response!


#4

You probably meant print, then.

Keep in mind you’ve been doing this all along, for example:

user_input = input()

Does exactly that, store the result of a function. And, to test that particular concept, use a much simpler function, not one that does user input! For example, one that adds two numbers.

It doesn’t necessarily need to be split up into multiple functions. If it’s supposed to be communicating with the user then yeah it’ll need to print and read and return a result. Things like this though looks super confused:

print(playerpick)
return playerpick

And outside of it you only ought to have:

print(coinpick())

And the ignored paramater.

…And that one branch which doesn’t return anything at all.

So some things don’t add up. The reason I ended up stripping everything is because I decided communication with a user is the most confusing thing, so that’s what I removed so that the rest could easier be dealt with (the question was about storing a result of a function)