Python Forum

Full Version: (Not urgent) IDLE issue with methods
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm in a fundamentals of computer science class at my school, and encountered an issue my teacher can't answer. I'm coding a method-based text adventure game with a "karma" system designed to give the user an ending. It looks something like this:

def karma():
    if italyStart() == True:
        karma = int(10)
I defined this method at the start of the code, and as the code proceeds I either add or remove one point from the integer. By the end, I have this display:

def theyShouldntBeHere():
    while True:
        print("Wait, you're not supposed to be here! This is back-stage!")
        print("I guess you can [stay], but I'd rather you [leave].")
        print("")

        i = input(">")
        if i == "stay":
            print("Okay, I guess you can stay. Let's see what you did while you were here...")
            print("You had ", karma(), " karma. Man, that's pretty ")
            if karma < 10:
                print("good.")
            elif karma > 10:
                print("screwed up.")
However, I get a consistent issue of karma not being recognized, and it instead returning the first line under "stay", but then looping back to the beginning of the program. If you notice any other issues, please note them as well!

PS: this is my first post to this forum. If I accidentally broke any rules, please tell me and I'll revise the post! Thanks for the help.
(Mar-22-2018, 06:50 PM)vinfer12 Wrote: [ -> ]
            print("You had ", karma(), " karma. Man, that's pretty ")
            if karma < 10:

Is karma a function or a variable?
(Mar-22-2018, 07:17 PM)nilamo Wrote: [ -> ]
(Mar-22-2018, 06:50 PM)vinfer12 Wrote: [ -> ]
            print("You had ", karma(), " karma. Man, that's pretty ")
            if karma < 10:

Is karma a function or a variable?

I was having issues with it identifying as a function. I need it to function as a variable that I can call to display in the text. It's identified as an integer currently.
I wish python.org would dump idle for distribution and replace with something like Thonny
I see that as an issue. You're treating it as if it's both a function and an int at the same time. In that snippet, for example, you call it like it's a function, and in the very next line do something that doesn't make sense for a function to do.

The only reason you should ever want it to be an int, is if you never want to call it as a function again. That is to say, just don't try to do both, it's so much easier to use a different variable name (or a return value).
I don't think there is an Idle issue here. You only need a global variable which value can be shared by the different functions. Don't use the same names for the functions and the variable. This can be done simply like this
karma = 0

def init_karma():
    global karma # <- use this line if the function contains:  karma = ... or karma += ... etc
    if italyStart() == True:
        karma = 10

init_karma()  # <- don't forget to call the function that initializes the variable

def theyShouldntBeHere():
    while True:
        print("Wait, you're not supposed to be here! This is back-stage!")
        print("I guess you can [stay], but I'd rather you [leave].")
        print("")
 
        i = input(">")
        if i == "stay":
            print("Okay, I guess you can stay. Let's see what you did while you were here...")
            print("You had ", karma, " karma. Man, that's pretty ")
            if karma < 10:
                print("good.")
            elif karma > 10:
                print("screwed up.")
This method works well as long as there are not too many global variables. In this case, you'll have to learn how classes work.