Python Forum
(Not urgent) IDLE issue with methods
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
(Not urgent) IDLE issue with methods
#1
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.
Reply
#2
(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?
Reply
#3
(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.
Reply
#4
I wish python.org would dump idle for distribution and replace with something like Thonny
Reply
#5
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).
Reply
#6
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Exclamation Python Homework (Urgent help needed!!) chickenseizuresalad 6 4,275 Oct-11-2021, 01:59 AM
Last Post: Underscore
Exclamation urgent , Python homework alm 2 2,301 May-09-2021, 11:19 AM
Last Post: Yoriz
  urgent I got a syntax errors alm 2 5,860 Feb-28-2021, 02:54 PM
Last Post: alm
Heart Urgent homework help needed Medou 4 2,714 Nov-24-2020, 09:28 AM
Last Post: buran
  [Urgent] build code GodMaster 2 1,800 Mar-23-2020, 12:25 AM
Last Post: jefsummers
  Bifid Genkey (Urgent) Laura123 2 2,042 Mar-09-2020, 08:09 PM
Last Post: micseydel
  Python Homework Help *Urgent GS31 2 2,581 Nov-24-2019, 01:41 PM
Last Post: ichabod801
  Need help! Please! Urgent! foxylen 1 2,293 Feb-27-2019, 05:50 PM
Last Post: buran
  Machine Learning Antivirus [Urgent] Echoo0o 4 4,896 Jul-28-2017, 01:57 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020