-2

I'm trying to get my balance to add up but I don't know how to save my balance to a variable from a random number. In the screenshot below, it shows that they are giving and my balance does not add up.

Steps taken:

I've tried passing bank instead of money through my functions

Read the python docs doesn't say anything about saving rand ints as variables

Tried if and then statement but same issue. My balance was not adding up.

Import different libraries did not work

enter image description here

import random
def main():
    bank=0
    backstory()
    pet=input("what pet do you have? ")
    print("nice! your pet will be a ", pet)
    decisions(pet, bank)
    #outcome()

def backstory():
    print('You are a homeless person and are trying to get money to feed yourself and your pet.')
    print('Everything you choose to do will effect how your outcome will be.')
    print('You have five decisions you can make')
    print('dont forget to eat or feed your pet so neither if you two will die!')

def decisions(animal,money):
    print('enter "beg" to beg for money')
    print('enter "work" to work for money')
    print('enter "eat" to eat food')
    print('enter "feed" to feed your pet')
    print('enter "steal" to steal from someone!')
    print('enter "skip" to do nothing and skip a decision for the day')
    cont=0
    bank=0
    while cont<=4:
        pick=input("what will be youre decision? ")
        if pick=="beg":
            beg(bank)
            cont+=1
        elif pick=="work":
            work(money)
            cont+=1
        elif pick=="eat":
            eat(money)
            cont+=1
        elif pick=="feed":
            feed(money)
            cont+=1
        elif pick=="steal":
            steal(money)
            cont+=1
        elif pick=="skip":
            skip(money)
            cont+=1
        else:
            print("sorry! thats not an option! please pick something from above")
    #outcome(animal, money)
    print("all done")

def beg(bank):
    names=["Alvin and the Chipmunks", "Bob", "Timmy", "Alex", "Carah", "A very Rich Man"]
    amount=random.randint(1,20)
    print(random.choice(names), "gave you ", amount, "!")
    bank=amount+bank
    print("your balance is ", bank)
main()
sinoroc
  • 18,409
  • 2
  • 39
  • 70
  • 2
    You might want to use a [debugger](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) for that or look at [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Andreas Dec 02 '19 at 05:07
  • the debugger shows no issues, except for work, eat feed, steal, skip functions i have yet not created. – dropkick_Tester Dec 02 '19 at 05:13
  • you have to return your `bank` value from each function (here `beg()`, ie, `return bank`), and update the `bank` variable value each time function is called, ie `bank = beg(bank)` – Shijith Dec 02 '19 at 05:14
  • Here's a short article titled [Tricky Python II: Parameter Passing for Mutable & Immutable Objects](https://medium.com/@tyastropheus/tricky-python-ii-parameter-passing-for-mutable-immutable-objects-10e968cbda35) that explains your problem with the beg function. – DarrylG Dec 02 '19 at 05:18
  • @Shijith Do you have another way of explaining it? Don't feel satisfied lol with how it was fixed – dropkick_Tester Dec 02 '19 at 05:42

1 Answers1

1

Because your bank is always local variable, in beg function , you don't make bank return, so bank in decisions function is always zero, so you need return it in beg function , like this:

def main():
    bank=0
    backstory()
    pet=input("what pet do you have? ")
    print("nice! your pet will be a ", pet)
    decisions(pet, bank)
    #outcome()

def backstory():
    print('You are a homeless person and are trying to get money to feed yourself and your pet.')
    print('Everything you choose to do will effect how your outcome will be.')
    print('You have five decisions you can make')
    print('dont forget to eat or feed your pet so neither if you two will die!')

def decisions(animal,money):
    print('enter "beg" to beg for money')
    print('enter "work" to work for money')
    print('enter "eat" to eat food')
    print('enter "feed" to feed your pet')
    print('enter "steal" to steal from someone!')
    print('enter "skip" to do nothing and skip a decision for the day')
    cont=0
    bank=0
    while cont<=4:
        pick=input("what will be youre decision? ")
        if pick=="beg":
            bank = beg(bank)
            cont+=1
        elif pick=="work":
            work(money)
            cont+=1
        elif pick=="eat":
            eat(money)
            cont+=1
        elif pick=="feed":
            feed(money)
            cont+=1
        elif pick=="steal":
            steal(money)
            cont+=1
        elif pick=="skip":
            skip(money)
            cont+=1
        else:
            print("sorry! thats not an option! please pick something from above")
    #outcome(animal, money)
    print("all done")

def beg(bank):
    names=["Alvin and the Chipmunks", "Bob", "Timmy", "Alex", "Carah", "A very Rich Man"]
    amount=random.randint(1,20)
    print(random.choice(names), "gave you ", amount, "!")
    bank=amount+bank
    print("your balance is ", bank)
    return bank

and then run it

main()

You will get it.

You are a homeless person and are trying to get money to feed yourself and your pet.
Everything you choose to do will effect how your outcome will be.
You have five decisions you can make
dont forget to eat or feed your pet so neither if you two will die!
what pet do you have? beg
nice! your pet will be a  beg
enter "beg" to beg for money
enter "work" to work for money
enter "eat" to eat food
enter "feed" to feed your pet
enter "steal" to steal from someone!
enter "skip" to do nothing and skip a decision for the day
what will be youre decision? beg
Alvin and the Chipmunks gave you  16 !
your balance is  16
what will be youre decision? beg
Alvin and the Chipmunks gave you  9 !
your balance is  25
what will be youre decision? beg
Alvin and the Chipmunks gave you  10 !
your balance is  35
what will be youre decision? beg
Alvin and the Chipmunks gave you  1 !
your balance is  36
what will be youre decision? beg
Carah gave you  13 !
your balance is  49
all done
Yixin Liu
  • 38
  • 5
  • Hi @Yixin I'm lost on your explanation, apologies. I got lost when you said i didnt return bank and so forth. Do you have another way of explaining it? Sorry for the hassle. I just don't feel satisfied for some fucken reason. I've read your explanation twice and still don't get it. – dropkick_Tester Dec 02 '19 at 05:31
  • In `decisions` function, `bank` is local variable, we named it `decisions_bank`. In `beg` function, `bank` is also local variable , we named it `beg_bank`. You call `beg` in `decisions`, and pass `decisions_bank` to `beg`.Then in `beg`, `decisions_bank` change to `beg_bank`, and you add a random number to `beg_bank`. At this time, `beg_bank` changed, BUT you don't retrun it to `decisions`, so `decisions_bank` is also zero, don't change. – Yixin Liu Dec 02 '19 at 05:51
  • maybe you can see it to know more of `Variables and scope`.https://python-textbok.readthedocs.io/en/1.0/Variables_and_Scope.html – Yixin Liu Dec 02 '19 at 05:53
  • You are a boss ass dude. Thank you for providing that documentation. I understand it now. I probably should've used a global variable instead of local. – dropkick_Tester Dec 03 '19 at 00:11