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))