1

I got this book to aide my learning, however even though it "covers" python 3, that's just a small section in the back which isn't much help until you are fairly proficient (I imagine) anyway, I'm in chapter 3 working with strings and my code is not working. I have found some issues in the books code and updated the code and I get it to start running now, but I get:

Traceback (most recent call last): File "C:/Users/Garan/Desktop/Portfolio/String Formatting.py", line 15, in print (format % (item_width, 'Apples', price_width, 0.4)) ValueError: unsupported format character '/' (0x2f) at index 2

I see no / character in the code. Maybe that is used to designate something else, I'm not sure. Here is my code below and hopefully someone can steer me down the correct road.

# Print a formatted price list with a given width
width = int(input('Please enter width: '))

price_width = int(10)
item_width = int(width - price_width)

header_format = '%-*s%*s'
format = '%-/s%*.2f'

print ('=' * width)
print (header_format % (item_width, 'Item', price_width, 'Price'))

print ('-' * width)

print (format % (item_width, 'Apples', price_width, 0.4))
print (format % (item_width, 'Pears', price_width, 0.5))
print (format % (item_width, 'Cantaloupes', price_width, 1.92))
print (format % (item_width, 'Dried Apricots (16 oz.', price_width, 8))
print (format % (item_width, 'Prunes (4 lbs.)', price_width, 12))

print ('=' * width)
Muertes
  • 11
  • 3

1 Answers1

3

You have an / here:

format = '%-/s%*.2f'

replace with:

format = '%-s%*.2f'
valentin
  • 3,498
  • 15
  • 23
  • Thank you so much! I guess I got too caught up with looking at the address the error pointed to. I should have looked at the parts it was calling. And that was wholly my fault too. the book has * there and I fat fingered the /. – Muertes May 23 '15 at 11:51