5 Replies - 2282 Views - Last Post: 12 July 2012 - 05:08 PM Rate Topic: -----

#1 blankwall  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 65
  • Joined: 12-July 12

Help with classes and doc comments

Posted 12 July 2012 - 02:40 PM

http://learnpythonth.../book/ex43.html

Good afternoon everyone. Im new to this site and looking for a little help. I have been working with zed shaws book learnpython thehardway and have come along pretty good. He gives extra credit but no answers to the extra credit and I am really stuck. I posted a link above so you guys can check out what Im talking about. Extra credit 2 is how to put a cheat into the little game code written. I have figured out a way for certain parts but Zed says you should be able to do it in 2 lines. The other main question I have is how do you use doc comments inside a class? I try saying functionname.__doc__ but it says the function doesnt exist. If i put quotes around it it gives me the definition of a string. Any help would be greatly appreciated. Thanks for your time.

Is This A Good Question/Topic? 0
  • +

Replies To: Help with classes and doc comments

#2 jared.deckard  Icon User is offline

  • New D.I.C Head

Reputation: 18
  • View blog
  • Posts: 46
  • Joined: 11-July 12

Re: Help with classes and doc comments

Posted 12 July 2012 - 03:50 PM

lol, after line 81 add:
print code

Was This Post Helpful? 0
  • +
  • -

#3 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 318
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Help with classes and doc comments

Posted 12 July 2012 - 03:57 PM

@jared.deckard Umm, what?

Question 2) How are you tackling it now? There is loads of solutions for a single problem. It say HE can do it in two lines, doing it in 2 lines or 20 lines doesn't make for a less valid solution.

A docstring inside a class? Maybe you are not instantiating the class and calling the class method, does this help?

class MyClass:

  def myfunction(self):
    """ This is the doc string for this method! """
    pass

...
# X is now an instance of "MyClass" object
>> X = MyClass()
>> print(X.myfunction.__doc__)
...
This is the doc string for this method!



If this isn't what you mean.. show us some code! I'm sure we can clear it up :)

This post has been edited by Simown: 12 July 2012 - 04:11 PM

Was This Post Helpful? 1
  • +
  • -

#4 jared.deckard  Icon User is offline

  • New D.I.C Head

Reputation: 18
  • View blog
  • Posts: 46
  • Joined: 11-July 12

Re: Help with classes and doc comments

Posted 12 July 2012 - 04:13 PM

View PostSimown, on 12 July 2012 - 03:57 PM, said:

@jared.deckard Umm, what?


I wasn't answering the main question.
The trick to adding cheats in one line with 2 words it to simply print the random number you are supposed to guess.
I think the author means 2 words on one line for each hard room.

That game is funny.
Was This Post Helpful? 1
  • +
  • -

#5 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 318
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Help with classes and doc comments

Posted 12 July 2012 - 04:15 PM

Ah, I completely skipped the actual code and read the questions. Completely out of context answer for me. My bad.

Yeah, that'd do nicely. Done it before I'm guessing? :D

This post has been edited by Simown: 12 July 2012 - 04:15 PM

Was This Post Helpful? 0
  • +
  • -

#6 blankwall  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 65
  • Joined: 12-July 12

Re: Help with classes and doc comments

Posted 12 July 2012 - 05:08 PM

Thanks for the help. Setting the class to a variable in this case X worked.
This is how i did it before.

  class Myclass(object): 
                    def function(self):
                             pass

print function.__doc__


That didnt work said function wasnt a function. But got it cleared up now.

As far as the cheat goes just putting print code is kind of cheesy. I thought of it more as like a real cheat in a video game. I set a value to be an auto correct for the code. Like if you typed 1000 it would automatically allow you into the next room. I had to use an elif and a couple other things but i like it a little more then just saying print.

This post has been edited by Simown: 12 July 2012 - 05:20 PM
Reason for edit:: Fixed code tags

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1