-3

I'm quite new to programing and after spending 3 hours trying to figure out why this was not working i couldn't.

import wget

print("Serverstall By Logix1") #Prints Info At start

print("Here comes the real code!")

class ServerSelector:
    def _init__(self, ServerType, PowerRequired):
        self.ServerType = USRchoice1
        self.PowerRequired = USRchoice2
    USRchoice1 = input("What server type to do want to create?")
    USRchoice2 = input("Please choose the amount of ram required?")
    def GarrysMod(self):
        if USRchoice1 is ("GarrysMod"):
            print("Nice! Great Choice!")

It doesn't print Nice Great Choice after entering GarrysMod? Anyone know whats wrong?

ForceBru
  • 43,482
  • 10
  • 63
  • 98
Logix
  • 13
  • 1
  • 3
    I wanted to suggest [this](https://stackoverflow.com/questions/132988/is-there-a-difference-between-and-is) as a possible duplicate, but turns out, you never call the function, so it's never executed – ForceBru Dec 18 '19 at 19:42
  • 2
    Welcome to stack overflow! If applicable, please [edit] your question to include the portion of code where you instantiate a `ServerSelector` object and call the `GarrysMod` method on it. If you don't have code that does that, then that's the issue – G. Anderson Dec 18 '19 at 19:43
  • 3
    Also note that it's kinda weird -- and probably not what you want to be doing -- to have statements inside a class that aren't attribute or method definitions. – cadolphs Dec 18 '19 at 19:44
  • @G.Anderson means that you never do something like `my_server_selector = ServerSelector(bla, bla)`. @Lagerbaer means that the `USRchoice1/2 = ...` lines are executed as the class is loaded, but the way this code is structured. nothing is done with the values. – Junuxx Dec 18 '19 at 19:47
  • Welcome to StackOverflow. See [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). We cannot effectively help you until you post your MRE code and accurately specify the problem. We should be able to paste your posted code into a text file and reproduce the problem you specified. Your post includes unrelated code, and you failed to include the code that uses the Class in any way: no instantiation, and you never call the function in question. This is not (yet) an issue of whether the function gets defined. – Prune Dec 18 '19 at 19:47
  • 7
    There's a lot that's wrong here. Step back and go over some tutorials about classes, or maybe avoid them entirely until you're more familiar with Python in general. – Alex Hall Dec 18 '19 at 19:47
  • @Prune: No, this probably is their actual full code that does reproduce the problem. – Junuxx Dec 18 '19 at 19:50
  • You _defined_ a function named `GarrysMod`, but you never _called_ it. – John Gordon Dec 18 '19 at 19:58
  • @Junuxx I'm afraid that might be the case; however, OP claims that execution *did* enter `GarrysMod`, which would require code that isn't posted here. – Prune Dec 18 '19 at 20:44

2 Answers2

2

the things USRchoice1 and 2 are not part of self, and you do not call the function. You are not instanciating an object anyways. So nothing happens.

import wget

print("Serverstall By Logix1") #Prints Info At start

print("Here comes the real code!")

class ServerSelector:
    def __init__(self, ServerType, PowerRequired):
        self.ServerType = ServerType
        self.PowerRequired = PowerRequired
        if self.ServerType is ("GarrysMod"):
            print("Nice! Great Choice!")

USRchoice1_tmp = input("What server type to do want to create?")
USRchoice2_tmp = input("Please choose the amount of ram required?")
My_selector = ServerSelector(USRchoice1_tmp, USRchoice2_tmp )

Therefore, not the existence of your function seems to be the problem, but your general structure of the code. Edit/Addition: the init has to be spelled __init__ with two _ on both sides. Notice your code above has only one _ in the beginning.

Grimmauld
  • 312
  • 2
  • 9
0

Python provides various methods to check if variables/methods are defined.

For Global Variables/Functions

def C():
   pass

MY_FUNC_NAME = "C"
NOT_MY_FUNC_NAME = "c"

print(MY_FUNC_NAME in locals()) # True
print(NOT_MY_FUNC_NAME in locals()) # False

For Class Methods/Variables

class MyClass:
   def C():
      pass


MY_METH_NAME = "C"
NOT_MY_METH_NAME = "c"

print(MY_METH_NAME in dir(MyClass)) # True
print(NOT_MY_METH_NAME in dir(MyClass)) # False
tbhaxor
  • 1,659
  • 2
  • 13
  • 43