Python Forum

Full Version: Function Not Working
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am making a text game. And what I am doing is using another python file as a module in the Main python file(Mod File). And The main file calls The Input_Main_Colony() function and I know it's going there because if I put a print("Passed") in the if statement it will print.But it won't run the Cheats function. And the base game bases what it does next from return codes. 0 means nothing happened 1 means something happened. But it won't pass either codes. It seems the program tries to run Cheat but it errors out.
(If you need me to explain something from my code ask me please)
def Input_Main_Colony(Input, ColonyVarbles):
    if Input == "cheat" or Input == "cheats":
        return Cheat(ColonyVarbles)
    return 0

def Cheat(Varables):
    C("cls")
    print("You can change the amount of a resource. Which one?")
    print("People\nMaterials\nPeople\nWood\nCASE SENSITIVE")
    ThingChange = input(": ")
    C("cls")
    print("How much of this resource do you want?")
    Amount = int(input(": "))
    Varables[ThingChange] = Amount
    return 1
try:
        Return = Mod.Input_Main_Colony(Input, Colony_Varables)
        time.sleep(2)
    except:
        pass
^^^^^This is how I'm running the Input_Mian_Colony() funtion from the main file
what does function C look like?
Place a print statement print("Returned") after line 7 to see if flow is returned
(Jul-19-2022, 02:33 AM)Larz60+ Wrote: [ -> ]what does function C look like?
Place a print statement print("Returned") after line 7 to see if flow is returned
It's not a function its a varable (C = os.system)
And the print("Returned") doesn't show
C is just another name for the os.system function. It is generally a bad idea to use single letter names for things, as they don't convey meaning. That lack of meaning makes code harder to read.
remove the bare try/except and see what the error is
we don't know what Input and Colony_Varables used when calling Mod.Input_Main_Colony are.
(Jul-19-2022, 05:25 AM)ndc85430 Wrote: [ -> ]C is just another name for the os.system function. It is generally a bad idea to use single letter names for things, as they don't convey meaning. That lack of meaning makes code harder to read.
Noted
(Jul-19-2022, 05:42 AM)buran Wrote: [ -> ]remove the bare try/except and see what the error is
we don't know what Input and Colony_Varables used when calling Mod.Input_Main_Colony are.
I don't know why I didn't think to remove the try/except but i figured out what it was. The C = os.system had ("cls") in the function/varabale so the c("cls") what really os.system("cls")("cls")
And the Input was just Input = str.lower(input(": ")
and the Colony_Varables was a dictionary
str.lower() is a method. Though you can call a method this way, classname.methodname(instance), it is unusual. The common way is like this:
Input = input(": ").lower()
This corresponds to instance.methodname().

Since you are starting out learning Python, now is the best time to develop good habits. I find your code difficult to read because you are not using conventions used by most Python programmers. You can learn about these conventions here:

https://pep8.org/