gpt4 book ai didi

python - 从新手到专业的Python代码不起作用

转载 作者:行者123 更新时间:2023-11-30 23:10:59 25 4
gpt4 key购买 nike

我买这本书是为了帮助我的学习,但是即使它“涵盖”了 python 3,这只是后面的一小部分,在你相当熟练(我想)之前没有太大帮助,无论如何,我在第 3 章中使用字符串,但我的代码无法正常工作。我在书籍代码中发现了一些问题并更新了代码,我让它现在开始运行,但我得到:

回溯(最近一次调用最后一次): 文件“C:/Users/Garan/Desktop/Portfolio/String Formatting.py”,第 15 行,位于 打印(格式%(item_width,'苹果',price_width,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)

最佳答案

这里有一个/:

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

替换为:

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

关于python - 从新手到专业的Python代码不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30412127/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com