Why do I need to use "str" in the first example, and not the second?

I am fairly new to learning coding at all, and am currently working on the Python3 course. I apologize if I use the wrong terminology or post incorrectly. Feel free to educate me if I do. My question is this:

In the first project, printing literal text and a variable works fine (last lines), but in the second project, the print line gives a syntax error unless I add “str” before the variable called “costground” . I am curious as to why, so I can prevent this error in the future. The only thing I can think of is that in the second project, I am printing a variable that is created by a calculation, and the first isn’t.

The link to the first project is here.

The link to the 2nd project is here.

I hope I have been clear, Thank you.

Project one:

#imports random number generator module

import random

#declares variables

name = "Brian"

question = "Will I grow a garden?"

answer = ()

#assigns variable to function, random generates number 1-9

random_number = random.randint(1,9)

#print(random_number) <--- uncomment to test random generator

#answer assigned to number from random number generator

if random_number == 1:

answer = "Yes - Definitely."

elif random_number == 2:

answer = "It is decidedly so."

elif random_number == 3:

answer = "Without a doubt."

elif random_number == 4:

answer = "Reply hazy, try again."

elif random_number == 5:

answer = "Ask again later."

elif random_number == 6:

answer = "Better not tell you now."

elif random_number == 7:

answer = "My sources say no."

elif random_number == 8:

answer = "Outlook not so good."

elif random_number == 9:

answer = "Very doubtfull."

else:

answer = "Error"

#prints question and answer, removes name if name is empty, if question is blank, prints warning message

if name == "":

print ("Question: " + question)

print (answer)

elif question == "":

print ("The Magic 8-Ball cannot provide a fortune unless you ask it something.")

else:

print (name + " asks: " + question)

print ("Magic 8-Ball's answer: " + answer)

Project 2


weight = (50)

#ground shipping
if weight <= 2:
 costground = weight *1.5 + 20
elif weight > 2 and weight <= 6:
  costground = weight * 3 + 20
elif weight > 6 and weight <= 10:
  costground = weight * 4 + 20
elif weight > 10:
  costground = weight * 4.75 + 20

print("Ground Shipping costs: " + str(costground))

It has to do with the + operator. When placed between two string values, it concatenates the values.
'Sit,' + ' Fido.' returns Sit, Fido.
When you use + between numbers, it adds them and returns the sum.
5 + 5 returns 10
When you attempt to use + between a string value and a number, an error is thrown. How would you evaluate 'Sit,' + 5 ?
The + operator can only be used between values of the same type, so using the str() constructor to convert the number to its string representation is necessary in your example.

2 Likes

Hi! welcome :smiley:

print ("Magic 8-Ball's answer: " + answer)

What you are doing is is called string concatenation which means that you are appending a string to the end of another string. I places the emphasis on string as it is only possible to do this with strings. We can’t for instance, concatenate a string (text) and an integer(number) together. To do this we first need to convert the integer to a string, which you do here:

print("Ground Shipping costs: " + str(costground))

In the second example, costground has the variable type float (a number with decimals) and as seen above we can not concatenate this to a string without converting it.

edit: @midlindner beat me to it :slight_smile:

2 Likes

Nothing wrong with an additional explanation.

1 Like

Thank you for your clear explanation. I understand now, and appreciate you taking the time to help!

1 Like

Thank you for your answer as well. I appreciate you taking the time to help.