python - pylint "invalid syntax"(语法错误)来自 '*='

标签 python while-loop pylint

我从 pylint 中收到语法错误,我相信该错误是由于使用 *= 引起的。我的原始代码仅使用 * 但这给了我错误的问题答案。我已经尝试了 *= 的多种变体,但没有一个起作用。 Pylint 说错误出现在第 8 行,尽管我认为错误出现在第 7 行。如果我只有 *,它就会通过 pylint 检查。

有什么建议吗?

 def num_doublings(initial_population, final_population):
    """prints how many days it takes for a population to reach a certain 
       number, if the population is doubling daily"""
    current_pop = initial_population
    num_days = 0
    while current_pop < final_population:
        increase = current_pop *= 2
        current_pop = current_pop + increase
        num_days += 1
    return num_days

最佳答案

使用 python 不可能做到这一点

increase = current_pop *= 2

这是一个替代方案

def num_doublings(initial_population, final_population):
    """prints how many days it takes for a population to reach a certain 
       number, if the population is doubling daily"""
    current_pop = initial_population
    num_days = 0
    while current_pop < final_population:
        current_pop *= 2
        increase = current_pop
        current_pop = current_pop + increase
        num_days += 1
    return num_days

关于python - pylint "invalid syntax"(语法错误)来自 '*=',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43932829/

相关文章:

python - 如何使用 matplotlib 一起绘制训练和测试图

python - 如何删除文本文件中关键短语之前的所有内容?

java - 输入对话框循环提前结束

macos - 无法安装模块 'docstring'

python - 在名称为连续数字的目录中循环

python - 如何将此数据集加载到 Pandas 中

python - 在 python 中使用 while 循环作为等待

c - While 循环 - 改变字符串变量

python - Pylint 无效的常量名

macos - Pylint 中的永久配置文件