从初级到专业的Python入门代码不起作用

2024-04-26 06:56:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我得到这本书来辅助我的学习,然而,尽管它“涵盖”了python3,那只是后面的一小部分,在你相当熟练(我想)之前没有太大帮助。无论如何,我在第3章处理字符串,我的代码不起作用。我在《代码》一书中发现了一些问题,并更新了代码,现在就可以开始运行了,但我得到了:

回溯(最近一次呼叫最后一次): 文件“C:/Users/Garan/Desktop/Portfolio/String格式.py“,第15行,in 打印(格式%(项目宽度,'苹果',价格宽度,0.4)) ValueError:索引2处不支持格式字符“/”(0x2f)

我在代码中看不到字符。也许那是用来指别的,我不确定。下面是我的代码,希望有人能引导我走上正确的道路。你知道吗

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

Tags: 文件字符串代码format宽度格式item字符