【问题标题】:Beginning Python from Novice to Professional code not working从新手到专业的 Python 代码不起作用
【发布时间】:2015-05-23 11:30:32
【问题描述】:

我得到这本书来帮助我的学习,但是即使它“涵盖”了 python 3,这只是后面的一小部分,在你相当精通(我想)之前没有多大帮助,反正我是在第 3 章中使用字符串,我的代码不起作用。我在书籍代码中发现了一些问题并更新了代码,现在我让它开始运行,但我得到了:

回溯(最近一次通话最后一次): 文件“C:/Users/Garan/Desktop/Portfolio/String Formatting.py”,第 15 行,在 打印(格式%(item_width,'Apples',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)

【问题讨论】:

    标签: python hints


    【解决方案1】:

    你在这里有一个/

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

    替换为:

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

    【讨论】:

    • 非常感谢!我想我太着迷于查看错误指向的地址。我应该看看它正在调用的部分。这也完全是我的错。这本书有 * 那里,我用手指发胖了 /。
    猜你喜欢
    • 1970-01-01
    • 2014-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-31
    • 1970-01-01
    相关资源
    最近更新 更多