in Python Practice Problems

Playing with Python “+” (plus) signs – Python addition and Concatenation

This python practice problem is a green level (easy).  The Python skills you will add to your tool belt are:

  1. Creating and assigning python variables
  2. Concatenation
  3. Addition
  4. Converting integers to strings
  5. Explicitly assigning a variable as a string or int

Problem Background
In this Python Problem you are having a conversation with Yoda, and he says, “Learn Python you will.  From previous conversations with Yoda you know it takes about 10 years to become a full blown Jedi Knight, but you want to know how many years in total you will have to study before becoming a Jedi, including your time as a Padawan.  You ask Yoda How long you will be a Padawan, and his reply is, “up to you, that is…”.

    1. Create a variable called yodaSays and assign the phrase, “Yoda says, Learn Python you will!”
    2. Create a second variable called yB4Jedi.
      1. Assign it to raw_input.
      2. The raw_input should ask the user  “How long will you be a Padawan?, Up to you that is…”
    3. Create a third variable and call it yB4Knight (Years before coming a Jedi Knight).  Assign the number 10.
    4. Create a fourth variable called totYrs.  Add yB4Jedi and yB4Knight and assign the total to totYrs.
    5. Concatenate the following:
      1. The variable yodaSays
      2. The string “It will take you “
      3. The variable totYrs.
      4. The string ” years to become a Jedi”
    6. Run the program.  It should prompt you to type the number of years that you will be a Padawan.  The first time type 5.
    7. Your program should print the following:

Yoda says, Learn Python you will!  It will take you 15 years to become a Jedi.

Gotya’s
The first time you run the program you will probably get errors like this…

  • totYrs = yB4Knight + yB4Jedi –TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’
  • TypeError: cannot concatenate ‘str’ and ‘int’ objects

You will have to play with converting integers to strings and vice versa. To do that just use the str() or int() built in functions.

To see the solution read more…

#
#
yodaSays = "Learn Python, you will!"

# note that the varaibale yB4Jedi must be a number before you can use addition.
# also notice that the raw_input is inside the int() function.
yB4Jedi = int(raw_input('How long will I be a Padawan?  Yoda says, up to you, that is.'))

# by not wrapping the number 10 in quotes like this "10" you are telling python that 10 is a number
# so there is no need to tell python that 10 is an integer.
yB4Knight = 10

# if you don't expicitly tell python that your raw_input is an int() you will get an error here.
totYrs = yB4Knight + yB4Jedi

# Python will not let you concatenate strings and numbers.  
# You must implicitly set totYrs to be a string before concatenation.
# also noteice that I added blank spaces inside my quotes.
# This will proveide the correct spacing between the words

print yodaSays + " It will take you " + str(totYrs) + " years to become a Jedi Master"