Python 3.8 - Попытка сохранить мои случайные числа в переменной - PullRequest
0 голосов
/ 02 декабря 2019

Я пытаюсь увеличить свой баланс, но не знаю, как сохранить свой баланс в переменной из случайного числа. На скриншоте ниже видно, что они дают, и мой баланс не складывается.

Предпринятые шаги:

Я пытался передать банк вместо денег через мои функции

Чтение в документации по Python ничего не говорит о сохранении рантовых целых как переменных

Пробовал если и тогда заявление, но та же проблема. Мой баланс не складывался.

Импорт разных библиотек не работал

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

1 Ответ

1 голос
/ 02 декабря 2019

Поскольку ваша bank всегда является локальной переменной, в функции beg вы не возвращаете bank, поэтому функция bank в decisions всегда равна нулю, поэтому вам нужно return вbeg функция, как это:

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

и затем запустите ее

main()

Вы получите это.

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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...