Skip to content
Home » PYTHON JUMPSTART COURSE Section 1 – Introduction, Lesson 10 – Comments

PYTHON JUMPSTART COURSE Section 1 – Introduction, Lesson 10 – Comments

Spread the love

We often want to add some information to our code to make it clear and easy to understand. It can be a great time-saver for other programmers who read our code as well as for our future selves. After a few months or years we may no longer remember what the code was supposed to do. If we have this information, things get clear in no time. The additional information that we add to our code is called comments. Comments are ignored by the interpreter, they are just for us.

We introduce comments with a hash sign (#). Everything following # up to the end of the line is treated as comments.

We place comments either before the line they refer to or at the end of the line.

comment

Example:

price1 = 20   # price on April 10th in dollars
price2 = 22   # price on July 2nd in dollars

# Calculate the difference in price between April and July.
difference = price2 - price1

There are no multiline comments in Python, unlike in many other languages.

But you can imitate them by using triple strings:

"""
This is a multiline string
but it can be treated as 
a multiline comment.
"""

print("hello")

You can also use triple single quotes. In the output the string is ignored, so it acts like a comment:

hello

QUIZ

1. We introduce comments with:
    A) a hash sign (#)
    B) an at sign (@)
    C) a double slash (//)
 
2. We place comments:
    A) after the line they refer to
    B) before the line they refer to
 
3. We can imitate multiline comments in Python by surrounding them with:
    A) ## and ##
    B) “”” and  “””
    C) /* and */
 

TRUE OR FALSE?

1) Comments are ignored by the interpreter.
2) Everything following # up to the end of the line is treated as a comment.

SOLUTION

QUIZ

1.We introduce comments with:
    A) a hash sign (#)
    B) an at sign (@)
    C) a double slash (//)
 
2. We place comments:
    A) after the line they refer to
    B) before the line they refer to
 
3. We can imitate multiline comments in Python by surrounding them with:
    A) ## and ##
    B) “”” and  “””
    C) /* and */
 

TRUE OR FALSE?

1) Comments are ignored by the interpreter.
True
2) Everything following # up to the end of the line is treated as a comment.
True

PROJECT

'''
Comments - Project

HOW RICH I AM
__________________________________________________________

Your task is to write a simple program that calculates how much money 
you should put on the last square of a chessboard and how long you would
have to work in order to earn this money, assuming that:

- You put one cent on the first square.
- You put 2 cents on the second square, 4 cents on the third square, and 
so on, so there's always double the amount of money on any given square
(except the first one) as on the preceding square.
- A chessboard consists of 64 squares.
- You're not interested in the total amount of money put on the chessboard,
you're only interested in the amount put on the last, 64th square.

Here are some more guidelines for you to follow:

1. As soon as the program starts, the user is asked to enter their yearly
income. This will be needed in the second part of the program.
2. The sum of money on the last square should be expressed in dollars.
3. The time needed to earn the money should be expressed in years.
4. The program should be commented.

And here's how you should write this program:

#1. Start by writing a multiline comment (string) with the following
basic information about the program:

The program calculates how much money you should put 
on the last square of a chessboard and how long you 
would have to work in order to earn this money.

#2. Write code that asks the user to enter their yearly income in dollars.
Save user input in a variable called income and precede the line of code
with a comment informing us that the user should enter their yearly
income in dollars.

#3. Calculate the amount of money on the last square in dollars.
As we are beginning with one cent on the first square, make the calculations
in cents and save the result in the variable last_square. Precede the line
with a comment explaining that last_square is the amount of money on the
last square expressed in cents.

Then convert the amount of money to dollars and save the result in the same
variable last_square. Use a comment at the end of the line informing us
that now the amount is in dollars.

Hints:
1. If the amount on square 1 is 1 cent, on square 2 - 2 cents, 
on square 3 - 4 cents, then 8, 16, 32 cents, and so on, you can use
the exponent operator. Just to remind you, the exponent operator is **, so
5 ** 3 = 125 for example.

2. If you have a variable x and want to assign a new value to it, using the
old value in your expression on the right, you can do directly like so:

x = x + 5
or 
x = 3 * x - x

It just evaluates the expression on the right and assigns it to the variable 
on the left, so the variable will now hold the new value. This topic will
be covered in more detail when we talk about assignment operators.

3. The amount of money on the first square is 1 cent, which is 2 to the power
of 0. On the second square we have 2 cents, so 2 to the power of 1. 
What's the exponent for the last square? Is it 64 or maybe slightly less?

#4. Print a message with the results. Use concatenation. The message 
should look like this:

There's going to be $X on the last square and you need Y years to earn it.

X and Y should be replaced by variables: X by last_square and Y should be
replaced by a variable called time_needed that you first have to create and
assign a value to, using an appropriate expression. Precede the line of 
code with a comment explaining what the time refers to and what unit 
is used for the time (years).

Remember that numbers must be converted to strings before you can use them
in concatenation. 

This time we'll need one more conversion. Whatever you enter as user input
from the keyboard is a string, even if it looks like a number. So is your
income that you entered at the beginning. If you want to use it in 
calculations, you first need to convert it to a number. There are basically
two options:

1. If the number is an integer, you can use the int() function.
2. If the number is a floating point number (a number with a decimal
point), you can use the float() function.

Either will work, but as we don't need so much precision, 
let's use the int() function to convert the income to an integer. 
So, if x is a string like for instance '45', then int(x) is
the number 45. 

There is one more thing we have to handle. When we were converting the
amount in cents to the amount in dollars, we made use of the division
operator (/) and the result assigned back to last_square is now a floating
point number. This is because the division operator works like this.

But this means that if we now convert last_square to a string in order to
concatenate it with other strings, it will be printed as a float, and
what's even worse, most probably in scientific notation 
(9.223372036854776e+18) which is not very readable for most users of
our program, so we should convert it first to an integer and then to
a string. We can use the following syntax: if x is a float, then:

int(x) - is an integer obtained by truncating down the float
str(int(x)) - is the string representation of the int obtained above.

The time_needed variable will be assigned a value coming from an expression
containing the division operator, so it will be a float. Convert it to
an integer, too, before converting it to a string in the concatenated string.
We just don't need to be that exact.

These topics will be soon discussed in more detail.

If the line with the print function is too long, you can span it over two or
more lines as long as you're within the parentheses.
'''

PROJECT SOLUTION

##1. 
'''
The program calculates how much money you should put 
on the last square of a chessboard and how long you 
would have to work in order to earn this money.
'''

##2. 
# User should enter yearly income in dollars.
income = input("Enter your yearly income in dollars: ")

##3. 
# money on last square cents
last_square = 2 ** 63
last_square = last_square / 100   # money in dollars

##4. 
# time needed to earn the money on the last square (in years)
time_needed = last_square / int(income) 

print("There's going to be $" + str(int(last_square))
     + " on the last square and you'll need about " 
     + str(int(time_needed)) + " years to earn it.")


Spread the love

Leave a Reply