-4

Here is my code, my issue is as the title above states, the input text does not appear and neither does the print below. I am new to Python so sorry for a simple mistake

class Horse:
    colour = ''
    height = ''
    speed = 0

    def __init__(self):
        self.speed = input("Enter an integer: ")

        if(self.speed != 0):
            self.gallop = (3 * self.speed)

        print(self.gallop)
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
Dylan
  • 17
  • 4
  • 3
    Where do you create `Horse` instance? – Łukasz Rogalski Nov 25 '19 at 11:56
  • The input and print values within the method are not being activated and I am unsure as to why – Dylan Nov 25 '19 at 11:56
  • 1
    Cannot reproduce - except the `input` statement should be wrapped with `int()` – CDJB Nov 25 '19 at 11:57
  • What would I use the Horse instance for? Would it not suffice that there is a class? I just want to pass these class variables to the method – Dylan Nov 25 '19 at 11:58
  • 2
    Have a read of [What __init__ and self do on Python?](https://stackoverflow.com/questions/625083/what-init-and-self-do-on-python) `self` represents an instance of `Horse`, and `__init__` is its constructor. If you don't create an instance, it will never be executed. – David Buck Nov 25 '19 at 12:02
  • 2
    Let me give you a constructive criticism. As a developer you'll always face problems you don't no how to solve. StackOverflow helps a lot with some issues, because there's a huge probability that you're not the first to deal with the problem, but the documentation of the language, framework or whatever you're using helps a lot either and should be your first method to solve problems. Always read the documentation, it's not there without a purpose. [Python Documentation](https://docs.python.org/3.7/tutorial/) – Felipe Endlich Nov 25 '19 at 12:10

3 Answers3

2

You need to make an instance of your object by initiating it. Here is the full program:

class Horse:
    colour = ''
    height = ''
    speed = 0

    def __init__(self):
        self.speed = input("Enter an integer: ")

        if(self.speed != 0):
            self.gallop = (3 * self.speed)

        print(self.gallop)

if __name__ == '__main__':
    x = Horse()
Faram
  • 525
  • 6
  • 21
  • 1
    Thanks, it worked but if I try to make an instance of the class at the top of the class before the method, it gives me an error. The error says the type is not defined. The actual class variable is not defined. – Dylan Nov 25 '19 at 12:11
  • You can't initiate something before it's declaration in procedural programming. Remember: Python supports multi paradigm programming. To avoid C like procedural execution, always put the code you want to execute into the `"if __name__ == '__main__':"` scope, as this is the OOP approach of execution. With the `"if __name__ == '__main__':"` usage, you can program in the typical C#/Java/Rust/Go way. Without the `"if __name__ == '__main__':"` it would execute your code in the typical C-like style (procedural town down execution). – Faram Nov 25 '19 at 13:57
1

You have to create an instance of the class... because so your code in the constructor is invoked

if __name__ == '__main__':
    a = Horse()
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
1

You did not create an horse instance. Add the code below.

def create_horse():
    new_horse = Horse() # this will ask for an integer

if __name__ == '__main__':
    create_horse()
Berkay
  • 1,029
  • 1
  • 10
  • 28