Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Oliver White
6,193 PointsCan I put a question unrelated to a treehouse course ?
I am working through the python OOP course -- I have tried to use elements of what I have learnt in the card mathing game and use it ( in a clumsy way) to practice and make a number generating class of my own -- i get stuck with the structure of classes methods and self." items" . I can get this to run on a single lump of code but I am still not getting whats wrong and the error message just says invalid syntax
5 Answers
Steven Parker
243,861 PointsThis is a very unconventional use of the __new__() method. Since it is a constructor it would normally return the new object instance, perhaps like this:
return super().__new__(self) # return the newly constructed object
But it's not returning a new object here. So the assignment of "forthewin" makes it a tuple and not a EuroWin object.
The code of __new__ seems to be initializing instead of constructing, so you may want to define it as __init__ instead, for which you would use a simple return with no arguments.
Either way, "forthewin" will then be a EuroWin object, and the print will work as expected.
Oliver White
6,193 Pointsimport random
class EuroWin:
lucky_numbers = []
picked_numbers = []
lucky_stars = []
picked_lucky_stars = []
num_of_balls = 50
num_of_stars = 12
def __new__(self):
self.lucky_numbers = list(range(1,self.num_of_balls+1))
self.picked_numbers.append(random.sample(self.lucky_numbers, k=5))
self.lucky_stars = list(range(1,self.num_of_stars+1))
self.picked_lucky_stars.append( random.sample(self.lucky_stars, k=2) )
return(self.picked_numbers,self.picked_lucky_stars)
def __str__(self):
return f"Your lucky numbers are {self.picked_numbers} and your lucky stars are {self.picked_lucky_stars}"
if __name__ == '__main__':
forthewin = EuroWin()
#print(forthewin.num_of_balls)
#print(forthewin.picked_lucky_stars)
#EuroWin()
print(forthewin)
Oliver White
6,193 PointsHell
I was just trying to put all my OOP learnings into place and make a small little fun task to pick some eruomillions numbesr I have done the terrible thing of getting stuck and hacking it to pieces and re-writting it in many ways -- I moved lthe iter to new as I learnt that I should not return things from an iter
I also started with loads of class attributes but have put them in the iter now -- I think I need to relearn decotrators or nested methods --
Anyway -- the big issue I have is I was trying to use the_str_ via print and I can get part or it to work but not all of it --
It brings the numbers but not the text -- other versons have brought the text and not the number --
Have a look -- I put it into visual Studio Code as it is better for debugging and following
The python is above
Oliver White
6,193 PointsThanks so much -- I will have a look at it
I am two thirds of the way through the intermediate OOP course and I was just trying to try out the different things I had learnt --
I had used the iter but was advised that should not be for a method that returned something -- I think I thought that setting the values was returning -- I have it in an extra tab on one of me Treehouse workspaces but the error messages are easier and more detailed in the VScode tab so I coppied it out -- I think my breaking down the requirements and choosing the correct OOP feature to use was the biggest issue -- SMILEY EMOJI -- will dig in and see if I can get it to work -- I cant promise it will generate the correct numbers but I will remember you !
Steven Parker
243,861 PointsThe workspace advantage it that the snapshot makes it easier on the helper to immediately replicate the issue, particularly if they do not have VSCode installed.
Oliver White
6,193 PointsThanks Steven -- Sorry -- I am doing the Beginning Data Science track and we just did the section on using Anaconda and all the other tools from there -- thats why I have been using VSCode --I just decided to do the OOP at intermediate to cement the learning I had done through the Begining to Data Science track
Steven Parker
243,861 PointsSteven Parker
243,861 PointsIf you are not already using a Treehouse Workspace, it would help if you transfer your project to one, and then you can include a link to a workspace "snapshot" with your question to make it easy for folks to replicate and analyze your issue. If you're not already familiar with that process, here's a video about sharing a snapshot of your workspace.