【问题标题】:how to check if items in list are power of any number in python?如何检查列表中的项目是否是python中任意数字的幂?
【发布时间】:2017-03-16 11:05:12
【问题描述】:

我什至不知道如何开始。 谁能帮我写代码?

【问题讨论】:

  • 这必须是家庭作业...另请参阅:stackoverflow.com/questions/40390902/…
  • 顺便说一句,通常你开始向你的教授或同学寻求帮助......(并参加所有讲座)
  • 这似乎更像是一道数学题,而不是编程题。找出数学过程后,用 Python 编码应该是显而易见的。

标签: python


【解决方案1】:

你显然是一个初学者,所以我将解释代码。假设我们有一个包含数字 1、5、7、9 和 16 的列表。

mylist = [1,5,7,9,16]

现在我们必须搜索我们的数字以确定它们是否是正方形。

for number in mylist:
    if math.sqrt(number)**2 == number:
        print(str(number) + " is a square number")

如果您只是按原样运行此代码,它将输出“错误:未定义数学”,您必须允许访问 math 模块。为此,请在代码类型的顶部:

import math

现在你可以运行它,它会输出:

1 is a square number
9 is a square number
16 is a square number

【讨论】:

  • 他要求在列表中找到平方数,所以我就这么做了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-12-21
  • 2020-04-02
  • 1970-01-01
  • 1970-01-01
  • 2011-11-03
  • 1970-01-01
相关资源
最近更新 更多