python - 当用户输入数字时确定丰富数字

标签 python python-2.7

当用户输入数字时,我尝试确定该数字是否充足。但我很困惑为什么它不起作用。自从我刚开始以来,不太擅长编码。任何帮助,将不胜感激。这是我到目前为止所拥有的:

def is_abundant():
    n = raw_input("Enter a number: ")
    max_divisor = int(n / 2) + 1
    sum = 0
    for x in range(1, max_divisor):
        if n % x == 0:
            sum += x
            print ("your number isn't Abundant")
is_abundant()

当我输入它时,它没有给我任何东西。请帮忙!

最佳答案

刚刚查了一下充裕数的定义:

In number theory, an abundant number or excessive number is a number for which the sum of its proper divisors is greater than the number itself. The integer 12 is the first abundant number. Its proper divisors are 1, 2, 3, 4 and 6 for a total of 16. — Wikipedia

想出了这个:

def isAbundant(n):
    factors = filter(lambda j: n % j == 0, range(1, n/2 + 1));
    return sum(factors) > n;

或者同样,如果你喜欢俏皮话,这个:

isAbdn = lambda n: sum(filter(lambda j: n % j == 0, range(1, n/2 +1))) > n;

现在,如果您坚持使用raw_input:

def isInputAbundant():
    n = int(raw_input("Enter a number: "));
    if isAbundant(n): print "YEAH! It's abundant.";
    else: print "Sorry! It's not abundant.";

希望这有帮助。

关于python - 当用户输入数字时确定丰富数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33603012/

相关文章:

python-2.7 - 如何将值传递给可插入 View ?

python - 如何将 BLAS 功能与 Python 的 Theano 库结合使用?

python - 如何使用 Python 验证特定的日期和时间格式

python - 使用 BeautifulSoup 查找具有两种特定样式的标签

python - 在 Python 中添加时间值

python-2.7 - Python Pandas : Obtaining count of non null values for a column using groupby. 聚合

Python 引用调用问题

python - numpy 的 digitize 函数可以输出均值或中值吗?

python - 无法在 pyspark 中应用 pandas_udf

python - Numpy:返回一个数组,其中 n 个条目最接近给定数字