-1

If I press build with following script:

if __name__ == "__main__":

    class Strom(object):
        def __init__(self):
            self.preis_kilowattstunde = 0.21


        def stromkosten_pro_jahr(watt):
            '''
            (integer) -> float 

            leitet aus einer Zahl in Watt eines Objektes die Stromkosten pro Jahr ab
            '''
            print(watt * brennstunden_im_tag * 365 * self.preis_kilowattstunde / 1000)

        def stromkosten_differenz(watt_objekt1, watt_objekt2):
            '''
            (integer, integer) -> float

            Nimmt die Stromkosten in Watt zweier Objekte und gibt die Stromkostendifferenz in CHF aus.
            '''
            if watt_objekt2 > watt_objekt1:
                print(stromkosten_pro_jahr(watt_objekt2) - stromkosten_pro_jahr(watt_objekt1))
            else:
                print(stromkosten_pro_jahr(watt_objekt1) - stromkosten_pro_jahr(watt_objekt2)

I get following error:

  File "/home/empedokles/Dropbox/Yannick/Python/stromkosten.py", line 31

         ^
SyntaxError: invalid syntax
[Finished in 0.2s with exit code 1]
[shell_cmd: python -u "/home/empedokles/Dropbox/Yannick/Python/stromkosten.py"]
[dir: /home/empedokles/Dropbox/Yannick/Python]
[path: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games]
  • @Seth: Thanks for the edit, but that wasn't what causes the error. The code button in that forum just won't work correctly on marked texts. – empedokles May 7 '14 at 22:16
  • I wasn't suggested what caused the error. Code button worked fine for me. – Seth May 7 '14 at 22:18
  • @Seth: You duplicated parts of my code. The duplication isn't the problem. – empedokles May 8 '14 at 13:53
  • I didn't duplicate any of your code, I just selected it and pressed the "code" button as you can see in the revision history. Sorry if it somehow caused confusion. Maybe you accidentally pasted it twice? – Seth May 8 '14 at 14:22
1

Your code is missing a closing ) on the last line, so if you try to run your code as-is you'll get an error from that.

However, assuming you fix that, running this code with Python 3 will complete successfully, but will not give any output. You are defining a class and a couple of methods, but you never instantiate the class or call any of the methods. Even if you did, though, you'd get an error as brennstunden_im_tag is not defined anywhere, so you'll get an error trying to run stromkosten_pro_jahr(). You also need to have self as the first parameter for any class method. Finally, stromkosten_pro_jahr() should return the value, not print() it. Here is an example of what your code should look like:

class Strom(object):
    def __init__(self, brennstunden_im_tag):
        self.preis_kilowattstunde = 0.21
        self.brennstunden_im_tag = brennstunden_im_tag

    def stromkosten_pro_jahr(self, watt):
        '''
        (integer) -> float

        leitet aus einer Zahl in Watt eines Objektes die Stromkosten pro Jahr ab
        '''
        return watt * self.brennstunden_im_tag * 365 * self.preis_kilowattstunde / 1000

    def stromkosten_differenz(self, watt_objekt1, watt_objekt2):
        '''
        (integer, integer) -> float

        Nimmt die Stromkosten in Watt zweier Objekte und gibt die Stromkostendifferenz in CHF aus.
        '''
        if watt_objekt2 > watt_objekt1:
            print(self.stromkosten_pro_jahr(watt_objekt2) - self.stromkosten_pro_jahr(watt_objekt1))
        else:
            print(self.stromkosten_pro_jahr(watt_objekt1) - self.stromkosten_pro_jahr(watt_objekt2))


if __name__ == "__main__":
    my_strom = Strom(10)
    my_strom.stromkosten_differenz(100, 50)

This returns 38.325 - hopefully that's the result you're looking for.

  • No, that is something intruduced by the one, who altered my posting. It's not the problem. I still get the same error. – empedokles May 8 '14 at 13:54
  • @user272441 The edit to your post did not change any of the code. Instead of using docstrings, try completely removing any bits of the code that you don't want to run, and try again. – MattDMo May 8 '14 at 14:02
  • @user272441 If the code in the question is not the code you are trying to run, please edit the question and put in the correct code. Once you have pasted in your code, highlight it all and hit Ctrl-K to properly format it as a code block. – MattDMo May 8 '14 at 14:15
  • Okay, corrected. – empedokles May 8 '14 at 14:20
  • 1
    @user272441 - yes, there is. If you haven't already, please install Package Control, then install SublimeREPL. – MattDMo May 8 '14 at 15:18
0

You should probably install a linter package to see what's wrong with your code, obviously the problem is in your code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.