简体   繁体   中英

Beginning Python from Novice to Professional code not working

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)

You have an / here:

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

replace with:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM