omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Syntax error def__init__(self, rare=False)

    Pythonista
    definit pythonista 3
    3
    6
    4072
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • craigmrock
      craigmrock last edited by craigmrock

      Trying to make a class with the following line of code that pythonista doesn’t like but works fine in idle
      def__init__(self, rare=False):
      Why doesn’t this work here?

      1 Reply Last reply Reply Quote 0
      • dgelessus
        dgelessus last edited by

        You're missing a space between the def and __init__.

        If that doesn't fix the error, can you post your entire code? (Or if your code is very long, only a few lines before and after the syntax error.) Sometimes when Python finds a syntax error, the location that it shows you isn't exactly where you made the error. This can happen when you forget to close a parenthesis for example, depending on your code, Python might not notice the missing parenthesis until a few lines later.

        craigmrock 1 Reply Last reply Reply Quote 0
        • craigmrock
          craigmrock @dgelessus last edited by

          @dgelessus thanks! That helped there. I got a new error message saying that
          heads is not defined
          At line 10, but I thought it was ok. There also appears to be errors at line 52 and 169 Here is my code below

          import random

          class Coin:
          def init(self,rare=False, clean=True, **kwargs):
          for key,value in kwargs.items():
          setattr(self,key,value)

          	self.is_rare = rare
          	self.is_clean = clean
          	self.heads = heads
          	
          	if self.is_rare:
          		self.value = self.original_value * 1.25
          	else:
          		self.value = self.original_value
          		
          	if self.is_clean:
          		self.color = self.clean_color
          	else:
          		self.color = self.rusty_color
          
          def rust(self):
          	self.color = self.rusty_color
          	
          def clean(self):
          	self.color = self.clean_color
          	
          def __del__(self):
          	print("Coin Spent!")
          	
          def flip(self):
          	heads_options = [True,False]
          	choice = random.choice(heads_options)
          	self.heads = choice
          	
          def __str__(self):
          	if self.original_value >= 1.00:
          		return "£{}Coin".format(int(self.original_value))
          	else:
          		return "{}p Coin".format(int(self.original_value * 100))
          

          class One_Pence(Coin):
          def init(self):
          data = {
          "original_value": 0.01,
          "clean_color": "bronze",
          "rusty_color": "brownish",
          "num_edges": 1,
          "diameter": 20.3,
          "thickness": 1.52,
          "mass": 3.56
          }
          super().init(**data)

          class Two_Pence(Coin):
          def init(self):
          data = {
          "original_value": 0.02,
          "clean_color": "bronze",
          "rusty_color": "brownish",
          "num_edges": 1,
          "diameter": 25.9,
          "thickness": 1.85,
          "mass": 7.12,
          }
          super().init(**data)

          class Five_Pence(Coin):
          def init(self):
          data = {
          "original_value": 0.05,
          "clean_color": "silver",
          "rusty_color": None,
          "num_edges": 1,
          "diameter": 18.0,
          "thickness": 1.77,
          "mass": 3.25,
          }
          super().init(**data)

          	def rust(self):
          		self.color = self.clean_color
          		
          	def clean(self):
          		self.color = self.clean_color
          

          class Ten_Pence(Coin):
          def init(self):
          data = {
          "original_value": 0.10,
          "clean_color": "silver",
          "rusty_color": None,
          "num_edges": 1,
          "diameter": 24.5,
          "thickness": 1.85,
          "mass": 6.50,
          }
          super().init(**data)

          	def rust(self):
          		self.color = self.clean_color
          		
          	def clean(self):
          		self.color = self.clean_color
          

          class Twenty_Pence(Coin):
          def init(self):
          data = {
          "original_value": 0.20,
          "clean_color": "silver",
          "rusty_color": None,
          "num_edges": 1,
          "diameter": 21.4,
          "thickness": 1.7,
          "mass": 5.0,
          }
          super().init(**data)

          	def rust(self):
          		self.color = self.clean_color
          		
          	def clean(self):
          		self.color = self.clean_color
          

          class Fifty_Pence(Coin):
          def init(self):
          data = {
          "original_value": 0.50,
          "clean_color": "silver",
          "rusty_color": None,
          "num_edges": 7,
          "diameter": 27.3,
          "thickness": 1.78,
          "mass": 8.0,
          }
          super().init(**data)

          	def rust(self):
          		self.color = self.clean_color
          		
          	def clean(self):
          		self.color = self.clean_color
          

          class One_Pound(Coin):
          def init(self):
          data = {
          "original_value": 1.00,
          "clean_color": "gold",
          "rusty_color": "greenish",
          "num_edges": 1,
          "diameter": 22.5,
          "thickness": 3.15,
          "mass": 9.5
          }
          super().init(**data)

          class Two_Pound(Coin):
          def init(self):
          data = {
          "original_value": 2.00,
          "clean_color": "gold & silver",
          "rusty_color": "greenish",
          "num_edges": 1,
          "diameter": 28.4,
          "thickness": 2.50,
          "mass": 12.00,
          }
          super().init(**data)

          coins = [One_Pence(), Two_Pence(), Five_Pence(), Ten_Pence(), Twenty_Pence(), One_Pound(), Two_Pound()]

          for coin in coins:
          arguments = [coin, coin.color, coin.value, coin.diameter, coin.thickness, coin.num_edges, coin.mass]
          string = "{}: - Color: {}, value:{}, diameter(mm):{}, thickness(mm):{}, number of edges:{}, mass(g):{}".format(*arguments)
          print(string)

          1 Reply Last reply Reply Quote 0
          • dgelessus
            dgelessus last edited by

            In line 10, you wrote self.heads = heads, but you haven't set the variable heads anywhere. (heads and self.heads don't mean the same thing - and if they did, that assignment wouldn't do anything.) I can't help much with that - I don't see any other place in your code where a heads variable is used.

            As for the other two errors - where did you get them from? Python doesn't usually give multiple errors at once. If you changed something in your code and that caused a different error, then it's helpful if you tell us what you changed. Also, always include the error message - just "line 52" doesn't tell us anything about what went wrong.

            When you're posting code, it also helps if you put it in a code block, so the forum displays it as code and doesn't read it as formatted text. The easiest way to do that is with the "</>" button in the toolbar when you reply. You can also do it by hand, by putting a line with ``` (three backticks) before and after your code.

            craigmrock 1 Reply Last reply Reply Quote 0
            • craigmrock
              craigmrock @dgelessus last edited by

              @dgelessus thanks again, I just deleted the heads line and it ran fine. I’ll be sure to include your reference tips in future posts too.
              Craig

              1 Reply Last reply Reply Quote 1
              • technoway
                technoway last edited by

                dgelessus - Good catch. I missed the missing space at first when I read the first post.

                That is def__init__-ly the problem.

                Sorry, I couldn't resist the bad pun.

                In my defense, I would have tried to help too, but I just got here.

                1 Reply Last reply Reply Quote 1
                • First post
                  Last post
                Powered by NodeBB Forums | Contributors