简体   繁体   English

用户输入nmber时确定大量数字

[英]Determining a Abundant Number when user inputs a nmber

I am try to determine if a number is abundant when a user inputs the number. 我试图确定用户输入数字时数字是否足够。 But I am so lost to why it isn't working. 但是我很迷茫,为什么它不起作用。 Not really good at coding since i just started. 自从我刚开始,就不太擅长编码。 Any help would be appreciated. 任何帮助,将不胜感激。 This is what I have so far : 这是我到目前为止所拥有的:

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()

It isn't giving me anything when I enter it. 输入时没有给我任何东西。 Please help! 请帮忙!

Just looked up the definition of an abundant number: 只是查询了很多数字的定义:

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. 整数12是第一个整数。 Its proper divisors are 1, 2, 3, 4 and 6 for a total of 16. — Wikipedia 它的适当除数是1、2、3、4和6,共16。— Wikipedia

Came up with this: 想到了这个:

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

Or equally, if you like one-liners, this: 或者同样,如果您喜欢单线,这是:

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

Now, if you insist on using raw_input : 现在,如果您坚持使用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.";

Hope this helps. 希望这可以帮助。

A couple of things. 有几件事。 First, you probably want to turn your input from a string into an integer as soon as possible, by replacing: 首先,您可能希望通过替换以下内容将输入从字符串尽快转换为整数:

n = raw_input("Enter a number: ")

with: 有:

import sys
:
try:
    n = int(raw_input("Enter a number: "))
except ValueError:
    print "Invalid number"
    sys.exit(1)

Second, you can't tell a number is abundant until after you've summed all its integral factors. 其次,你不能告诉一个号码是丰富的,直到你总结出所有的组成因素 So your code will need to sum them first, then check: 因此,您的代码需要先将它们求和,然后检查:

for x in range(1, max_divisor):
    if n % x == 0:
        sum += x
if sum > n:
    print ("Your number is abundant")
else:
    print ("Your number isn't abundant")

So, in toto: 因此,toto:

import sys

def is_abundant():
    try:
        n = int(raw_input("Enter a number: "))
    except ValueError:
        print "Invalid number"
        sys.exit(1)
    max_divisor = int(n / 2) + 1
    sum = 0
    for x in range(1, max_divisor):
        if n % x == 0:
            sum += x
    if sum > n:
        print ("Your number is abundant")
    else:
        print ("Your number isn't abundant")

is_abundant()

One other thing to keep in mind though this is more of a stylistic thing. 请记住另一件事,尽管这更多是一种风格问题。 Many people would consider the function is_abundant() as one taking an integer and returning a Boolean value, rather than one also responsible for the input of the number and output of the results. 许多人会认为函数is_abundant()是一个采用整数并返回布尔值的函数,而不是同时负责数字输入和结果输出的函数。

You may want to keep that in mind in future, in order to "properly" structure/name your code so as to make more sense, such as: 您可能希望将来记住这一点,以便“正确地”对代码进行结构/命名以使其更有意义,例如:

import sys

def is_abundant(num):
    max_divisor = int(num / 2) + 1
    sum = 0
    for x in range(1, max_divisor):
        if num % x == 0:
            sum += x
    return sum > num

try:
    n = int(raw_input("Enter a number: "))
except ValueError:
    print "Invalid number"
    sys.exit(1)

if is_abundant(n):
    print ("Your number is abundant")
else:
    print ("Your number isn't abundant")

“ n”变量是字符串,您应该在第3行和第6行将其转换为整数:::> int(n)祝您好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM