Clay Buxton
Clay Buxton

Reputation: 11

Variables don't change after being run through a function

I'm writing a small game in python in which certain events happen and effect variables which need to stay in certain parameters. I have the main file and then another file which has all of the events in them. Some of the values are changed in the function then supposed to change the overall values in main (Sorry if that doesnt make sense)

Here's the part in main:

while (army > -100 and army < 100 and people > -100 and people < 100 and church > -100 and church < 100 and affairs > -100 and money < 100 and money > -100):
    os.system('clear')
    #Top Bar. Should Stay throughout game.
    print("[-]==[King: " + king + "]==[Years in power:" + str(years) +"]==[Army: " + str(army) + "]==[People: " + str(people) + "]==[Church: " + str(church) + "]==[Foreign Affairs: " + str(affairs) + "]==[Economy: " + str(money) +"]==[-]")
    print(people)
    event1(army, people, church, affairs, money, years)

That loops until one of the parameters falls below 0 then there's losing conditions

Right now there is only one event, and it's not quite finished, but I only need one thing to at least see a change in the values.

Here that:

def event1(army, people, church, affairs, money, years):
    #Feilds are Flooding
    print("")
    print("Sire! The Feilds in the eastern baronies are flooding! What should we do?")
    print("")
    print("Choices:")
    print("1: The rain will pass, do nothing. (~Money, People)")
    print("2: Have the Royal Builders build flood protection! (~Money, People)")
    print("")
    c=input("Your choice sire: ")
    while True:
        if c > 2:
            print("")
            print("Please chose a valid option")
            print("Your choice sire: ")
            continue
        if c == 1:
            time.sleep(2)
            print("")
            print("You do nothing, your people starve from flooded feilds (-People, +Money)")
            money = money+20
            people = people-20
            years = years+1
            raw_input("Press Enter to go to the next year")
            return money
            return years
            return people
            break

After it runs the event the values people, money and years are all supposed to change, but when it loops, nothing changes.

Any help is appreciated! Thank you!

Upvotes: 0

Views: 78

Answers (2)

SnootierMoon
SnootierMoon

Reputation: 137

YOU DO NOT NEED THE RETURN!!!!!!!!!!!!!!!!!!!! COMPLETELY REMOVE IT! READ THIS! (should help)

The return in fact ruins your command, and there is a really easily explainable way to understand how it works.

First I need to explain something, because I am kind of confused about your code. Return is used to make the value of your command whatever you have returned. Return is used like this:

def AddThreethousandTwohundredSeventyNineToNum(num):
    num = num + 3279
    return num

and then

print AddThreethousandTwohundredSeventyNineToNum(4)

It should print "3283". (3279 + 4)

print printThreethousandTwohundredSeventyNineToNum(2)

It will print "3281".

You can also do cool things like:

if AddThreethousandTwohundredSeventyNineToNum(x) == y:  
     DoSomething

All return does is make the value OF THE FUNCTION whatever you want it to be. In the last code, the function looks for what I made num, and sees that it is 4 or 2, so it does num = num + 3279, so num gets increased by 3279 (3273 or 3271). When you do return num, it makes THE FUNCTION equal num.

That means what you have done is changed all those beatiful values in lines 21-23 (money, people, etc.) and technically that is all you had to do. However, when you returned the numbers, you made your command not only change those values, but also become a number, and obviously you cant just have a number lying around in your command. Or else the interpreter will not understand.

I hope I was clear enough, and if not, please please PLEASE tell me (please).

Upvotes: 0

Jean-Fran&#231;ois Fabre
Jean-Fran&#231;ois Fabre

Reputation: 140186

Those are local variables. As soon as you leave the method scope, the value is lost, unless you return and actually use the returned values.

In your caller, assign your variables with the new returned values:

money, years, people = event1(army, people, church, affairs, money, years)

and in event1, perform only one return (others are unreachable) of the tuple containing the 3 values you want to return (which is unpacked to the 3 upper level eponymous variables):

return money, years, people

Upvotes: 3

Related Questions