-2

I don't know what's wrong with my code. It would just not execute. Nothing happens, no errors occur. I can't figure it out. If someone can tell me what I have done wrong, please do so and I will be grateful.

class Money (object): def __init__ (self, euro, cent): self.euro = euro self.cent = cent def __str__ (self): if self.cent >= 100: r = self.cent / 100 self.cent = self.cent % 100 self.euro = self.euro + r return ("%d EUR & %d cents") % (self.euro, self.cent) else: return ("%d EUR & %d cents") % (self.euro, self.cent) def changeCent (self): #100 c = 1 E cents = self.euro * 100 self.cent = self.cent + cents return self.cent def changeSum (self, euros): #1 E = 100 c euros = self.cent / 100 self.euro = self.euro + euros return self.euro def debt (self, years, rate): value = Money() multiply = rate * years * 12 / 100 value.euro = self.euro * multiply value.cent = self.cent * multiply if value.cent > 100: euro_ = value.cent / 100 value.cent = value.cent - 100 value.euro = value.euro + euro_ return value def main(): x = Money() x.euro = int(input("Type in your EURO ammount: \n")) x.cent = int(input("Type in your CENT ammount: \n")) print (x) 
6
  • 2
    You've defined a class and a main function. Are you running the function? Commented May 24, 2018 at 13:12
  • You should have started with a Hello World program and extended it from there. Commented May 24, 2018 at 13:13
  • Uhm, I dunno. I thought 'def main()' was to create a border between the class and its methods/functions and the main program itself. Commented May 24, 2018 at 13:14
  • 1
    Are you thinking of main from C/C++? Commented May 24, 2018 at 13:15
  • 1
    @AngelDraghici A class is something that helps organize/encapsulate logic inside of a script - same with methods (like main()). But for the script to run, you have to call some method, or class property. Otherwise, you've written a function library - not a script that does anything on its own. Commented May 24, 2018 at 13:15

5 Answers 5

6

In python, main is not a special function (unlike C, for example).

You need to explicitly call main() in your script.

A common idiom is to only do this if your file is run as a script (as opposed to being imported):

if __name__ == "__main__": main() 

See the documentation for how this works

Sign up to request clarification or add additional context in comments.

Comments

2

You defined the main function, but you never call it.

add main() to the end of your code.

It then runs but there are errors, to fix the errors, change your main() function to

def main() euro = int(input("Type in your EURO ammount: \n")) cent = int(input("Type in your CENT ammount: \n")) x = Money(euro, cent) print (x) 

Full working code:

class Money (object): def __init__ (self, euro, cent): self.euro = euro self.cent = cent def __str__ (self): if self.cent >= 100: r = self.cent / 100 self.cent = self.cent % 100 self.euro = self.euro + r return ("%d EUR & %d cents") % (self.euro, self.cent) else: return ("%d EUR & %d cents") % (self.euro, self.cent) def changeCent (self): #100 c = 1 E cents = self.euro * 100 self.cent = self.cent + cents return self.cent def changeSum (self, euros): #1 E = 100 c euros = self.cent / 100 self.euro = self.euro + euros return self.euro def debt (self, years, rate): value = Money() multiply = rate * years * 12 / 100 value.euro = self.euro * multiply value.cent = self.cent * multiply if value.cent > 100: euro_ = value.cent / 100 value.cent = value.cent - 100 value.euro = value.euro + euro_ return value def main(): euro = int(input("Type in your EURO ammount: \n")) cent = int(input("Type in your CENT ammount: \n")) x = Money(euro, cent) print (x) main() 

2 Comments

@AngelDraghici No problem! :)
@AngelDraghici If my answer solved your problem, please accept my answer with the green tick mark near the upvote and downvote buttons. Thank you! :D
2

The pythonic way of getting a result here would be to add following lines to the file:

if __name__=='__main__': main() 

It checks, whether you are calling the script as the main script. Currently you only define the function main, but you never call it.

Comments

1

You should run the function main()

Comments

0

To execute the function type main() at the bottom. You did almost everything except calling the function.

One more mistake

your constructor def __init__ (self, euro, cent):expects two parameters. You are not passing any value to it.

To fix that change to:

def __init__ (self, euro=0, cent=0): 

which assigns default value of 0 to both euro and cent

2 Comments

Thank you, misunderstanding the main() method with the main from C/C++.
Adding the euro=0, cent=0 Is not the right solution

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.