Python Forum
Adding a paragraph of text before an exercise - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Adding a paragraph of text before an exercise (/thread-9931.html)



Adding a paragraph of text before an exercise - oldDog - May-05-2018

I have only just started to learn Python and hope someone will be kind enough to help me. I am reading and doing the exercises from the book "Learn Python 3 the Hard Way" and before each exercise i would like to add a paragraph of text which explains in my own words what the exercise is all about. I don't want to add the '#' character because that will comment everything out and only i will be able to see the text. When i tried using triple quotes i got an error. I don't think putting 'print' before each and every line is the answer either. Would someone help me out here please. Big thanks


RE: Adding a paragraph of text before an exercise - Gribouillis - May-05-2018

Triple quotes should not throw an error. Can you post the whole error message (exception traceback)
''' This exercise is about adding triple quotes
in a python program. A triple quoted string is
a literal expression. As such it is an expression statement.
It can appear anywhere in the program'''



RE: Adding a paragraph of text before an exercise - snippsat - May-05-2018

(May-05-2018, 06:43 AM)oldDog Wrote: I don't want to add the '#' character because that will comment everything out and only i will be able to see the text.
Comment # is meant for you and other who read your code.

There is way with docstring in function and class.
Then can look at what's in docstring with help() and __doc__.
Example:
def foo():
    '''The meaning of life, the universe, and everything'''
    return 42
Usage:
>>> help(foo)
   
Help on function foo:

foo()
    The meaning of life, the universe, and everything

>>> foo.__doc__
'The meaning of life, the universe, and everything'

>>> foo()
42

Class:
class Bar():
    '''
    This class dos This
    Need to explain more
    '''
    var = 999

    def run(self):
        '''This method do run something'''
        pass
Usage:
>>> Bar.__doc__   
'\n  This class dos This\n  Need to explain more\n  '
Output:
>>> help(Bar) Help on class Bar in module builtins: class Bar(object) | This class dos This | Need to explain more | | Methods defined here: | | run(self) | This method do run something | | ---------------------------------------------------------------------- | Data descriptors defined here: | | __dict__ | dictionary for instance variables (if defined) | | __weakref__ | list of weak references to the object (if defined) | | ---------------------------------------------------------------------- | Data and other attributes defined here: | | var = 999



RE: Adding a paragraph of text before an exercise - oldDog - May-05-2018

Hi Gribouillis
I was doing an exercise and thought i'd add a paragraph explaining what the exercise was all about. but, although the exercise was done correctly the paragraph of text does not appear on the screen?


RE: Adding a paragraph of text before an exercise - Gribouillis - May-05-2018

(May-05-2018, 11:39 AM)oldDog Wrote: the paragraph of text does not appear on the screen?
You need to call the print() function to see the paragraph in the output:
print("""
    This is a paraghaph with
    my explanations.
""")
You can also do
para = """This new paragraph
explains things even better
"""
print(para)



RE: Adding a paragraph of text before an exercise - oldDog - May-22-2018

Hi Gribouillis
Problem solved now mate.
Big thanks for the help and advice