Python:如何使用f字符串进行数学运算
我正试图使用python 3.6的新f-string功能编写我自己的99瓶啤酒墙实现,但我被卡住了:Python:如何使用f字符串进行数学运算,python,python-3.6,f-string,Python,Python 3.6,F String,我正试图使用python 3.6的新f-string功能编写我自己的99瓶啤酒墙实现,但我被卡住了: def ninety_nine_bottles(): for i in range(10, 0, -1): return (f'{i} bottles of beer on the wall, {i} of beer! You take one down, pass it around, {} bottles of beer on the wall') 如何在最后一对括
def ninety_nine_bottles():
for i in range(10, 0, -1):
return (f'{i} bottles of beer on the wall, {i} of beer! You take one down, pass it around, {} bottles of beer on the wall')
如何在最后一对括号中减少“I”?我尝试了I-=1,但没有结果(语法错误).您正在那里查找
{I-1}
i-=1
是f字符串中不允许的语句
除此之外,您不应该从函数返回;这只会导致执行循环的的第一次迭代。相反,要么打印,要么创建一个字符串列表并将其连接起来
最后,考虑将瓶子的起始值传递给<代码> 99枚瓶子> <代码>。
总之,请使用以下各项:
def ninety_nine_bottles(n=99):
for i in range(n, 0, -1):
print(f'{i} bottles of beer on the wall, {i} of beer! You take one down, pass it around, {i-1} bottles of beer on the wall')
你正在那里寻找{i-1}
i-=1
是f字符串中不允许的语句
除此之外,您不应该从函数返回;这只会导致执行循环的的第一次迭代。相反,要么打印,要么创建一个字符串列表并将其连接起来
最后,考虑将瓶子的起始值传递给<代码> 99枚瓶子> <代码>。
总之,请使用以下各项:
def ninety_nine_bottles(n=99):
for i in range(n, 0, -1):
print(f'{i} bottles of beer on the wall, {i} of beer! You take one down, pass it around, {i-1} bottles of beer on the wall')
{i-1}
应该这样做。{i-1}
应该这样做。