Hello. I am stuck on a simple piece of code. For some reason it keeps saying it can't call the Str object. Im also unsure on how to float the data to 2 decimal points (i don't know where the 2.f% should go.) The task is to make a taxi fare calculator based on the given distance. It costs £1.20 to start with, for every km up to 10 its 70p and over 10 an extra 30p per km. Thank you.

def taxiFare():
    distance = input("How long is the journey to your desired destination?")
    start = 1.20
    if distance <=10:
        print "the total journey price will be"  (distance*0.7)+start
    elif distance >10:
        print "the total of the journey will be" (((distance - 10)*30)+8.20)

When printing multiple things, seperate them with a comma. For instance:

distance = 15
print "The distance is", distance
#Output: The distance is 15

However, there's a better was to do it:

distance = 2.873430192
print "The distance is %.2f miles." % distance
#Output: The distance is 2.87 miles.

Just put the %d or %f or %.2f or %s or whatever inside of the string, then follow the string with a %, then put your variable there. If you have more than on thing you want to put in a string, put your variables as a tuple at the end.

a = 15
b = 20
print "The two variables are %d and %d." % (a,b)
#Output: The two variables are 155 and 20.

There are some other neat tricks you can do with this, but I'll leave that to you to find out if you care.

Btw, you don't just have to list a variable at the end, you can do operations to it, too.

print "The total journey price will be %.2f" % distance*0.7+start

Edited 6 Years Ago by Mathhax0r: n/a

You need to check your calculations. You are charging the poor customer 30 Pounds per km for traveling a distance above 10 km. You really want to charge him 1 Pound (0.7+0.3) per km. Also, you need to add a 10 mile short price to the distance-10 long distance price

This article has been dead for over six months. Start a new discussion instead.