-3

Very new to programming and trying to write a Wheel of Fortune game using Python. One of the features is a dictionary that contains all of the possible wheel slices. A function will, among other things, grab a random key and return that value. The dictionary:

wheel = {
    "Bankrupt": bankrupt(),    # a function that removes all the player's money
    "$100": 100,
    "$200": 200,
    "$300": 300,
    "$400": 400,
    "$500": 500,
    "$600": 600,
    "$700": 700,
    "$800": 800,
    "$900": 900,
    "$1000": 1000,
    "$2000": 2000
}

I'm sure this is a really newbie question but I cannot understand why the bankrupt function within the dictionary is being called as soon as I run the script, before I've actually called it via that other function which selects a random wheel slice.

Any guidance would be much appreciated.

dblinkhorn
  • 41
  • 7
  • Well you're calling it. – superb rain Aug 13 '20 at 04:55
  • You are calling bankrupt() and its result is then placed in the dictionary. – Karl Aug 13 '20 at 04:56
  • I guess I am! I guess I really should be asking how do I call this function only when I want to while still being able to call the other values using the same "spin = random.choice(list(wheel.values()))"? – dblinkhorn Aug 13 '20 at 04:57
  • The question in your comment is a tautology. You are the programmer; the function is called when you call it. Call it wherever in your program you want. Your posting does not describe the problem flow, nor did you supply your logic. Please repeat [on topic](https://stackoverflow.com/help/on-topic) and [how to ask](https://stackoverflow.com/help/how-to-ask) from the [intro tour](https://stackoverflow.com/tour). – Prune Aug 13 '20 at 05:05
  • For more focus -- please supply the expected [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). Show where the intermediate results differ from what you expected. – Prune Aug 13 '20 at 05:06
  • 2
    Thank you all for your input and for how to be a better user of Stack Overflow questions. I think I solved the issue by simply changing the value to "bankrupt" and using an if statement in the other function that picks a random value from this dictionary to actually call the function. I apologize if this is such a remedial question, but sometimes I get stumped on the simplest of things. – dblinkhorn Aug 13 '20 at 05:10
  • The previous comment is the answer accepted by the OP. Please close the question as a duplicate. – Trenton McKinney Aug 13 '20 at 06:08

1 Answers1

0

When python creates your dictionary it will evaluate each value. Therefore it runs bankrupt () to find the value of the bankrupt key. All the other values are also being evaluated i.e 100 is evaluated to 100.

In python functions are 'first class' objects so you can pass them around like any other object. This means you can pass it as bankrupt to give it the function or as bankrupt() to give it the result of the bankrupt function. You could also have multiple possible bankrupt functions the choice of which is defined by a wrapper function. In this case you might want to use bankrupt () as your dictionary key because the correct function is chosen when the dictionary is initialised.

It is also worth noting this will happen elsewhere in python such as function arguments when the function is initialised.

sam
  • 1,005
  • 1
  • 11
  • 24