Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:如何使用f字符串进行数学运算_Python_Python 3.6_F String - Fatal编程技术网

Python:如何使用f字符串进行数学运算

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') 如何在最后一对括

我正试图使用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')

如何在最后一对括号中减少“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}
应该这样做。