0

I am learning to write python and I am writing some scripts according to what a book says. The code is as follows

cars = 100 space_in_a_car = 4 drivers = 30 passengers = 90 cars_not_driven = cars - drivers cars_driven = drivers carpool_capacity = cars_driven * space_in_a_car average_passengers_per_car = passengers / cars_driven print "There are % cars available." % cars print "There are only % drivers available." % drivers print "There will be % empty cars today." % cars_not_driven print "We can transport % people today" % carpool_capacity <--Line 24 print "We have % to carpool today." % passengers print "We need to put about % in each car." % average_passengers_per_car 

It says that on line 24, which is the line that says that we can transport % people today, has an error. The Terminal says that there is an unsupported format character "p", but I cannot seem to find the problem. All of the lines follow the exact same format so when I look over the code I cannot see the problem. I need to know what the problem is.

0

1 Answer 1

4

The print statements you included are not valid. Using string formatting with the % sign requires that you provide a format character after the percent. This page explains it pretty well.

This is one way to do it:

cars = 100 space_in_a_car = 4 drivers = 30 passengers = 90 cars_not_driven = cars - drivers cars_driven = drivers carpool_capacity = cars_driven * space_in_a_car average_passengers_per_car = passengers / cars_driven print "There are %d cars available." % cars print "There are only %d drivers available." % drivers print "There will be %d empty cars today." % cars_not_driven print "We can transport %d people today" % carpool_capacity print "We have %d to carpool today." % passengers print "We need to put about %d in each car." % average_passengers_per_car 

So, either you copied from the book incorrectly, or the book is wrong, or the book provided the above code as an exercise for you to modify it so that it will run properly. That said, this way of doing string formatting in python is very old -- there are more modern ways of achieving this nowadays that you can read about in the documentation for your version of python.

Sign up to request clarification or add additional context in comments.

1 Comment

OK thank you. I think that the problem was that I didn't understand that I needed a format character after the % sign.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.