Python Forum

Full Version: Can you give me a code based on the given skeleton code?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I am given a problem for digit powers from project Euler problem 30 where I have to write code using oops.
The skeleton code is:
class digitpower():
    def __init__(self):
    #add code here
    def isarmstring(self,number):
    #add code here
    def sumofarmstrongnumbers(self,start,end):
    #add code here
We aren't here to write code for you.
What have you tried? We are glad to help, but we are not here to do your homework for you.
Please, post your code (in code tags) and ask specific questions. Don't forget to include the full traceback (in error tags) if you get one.
Also read https://python-forum.io/misc.php?action=help&hid=52
The code which I wrote gave me an error - Attribute error: no attribute for start.
class digitpower():
    def __init__(self):
        pass
    def isarmstrong(self, number):
        temp = self.number
        a = list(map(int,str(n)))
        b = list(map(lambda x: x ** 5,a))
        if sum(b) == self.number:
            return True
        return False
    def sumofarmstrongnumbers (self,start,end):
        total = 0
        for i in range(self.start,self.end):
            if i == digitpower.isarmstrong(i):
                total += i

        return total
value = digitpower()
print(value.sumofarmstrongnumbers(10000,99999))
On line 13 you refer to self.start and self.end. However your class has no attribute start and end. You are supposed to use start and end - these are the arguments you pass when call digitpower.sumofarmstrongnumbers() method.
Note that you make same error on lines 5 and 8 with self.number

All that said it's obvious that skeleton class is not really well designed to warrant it to be a class at all
But even after doing that, there is an error.
TypeError: missing a positional value number
now, that's the place to use self, not digitalpower

Please, post the entire traceback that you get. We need to see that whole thing. Do not just give us the last line.
Take a time to read What to include in a post

Also, you need to learn how to read the traceback and debug your code yourself.
Why have the class in the first place? Those two functions look like they're entirely separate and standalone. The class just adds unnecessary complexity.
(Mar-22-2020, 10:45 AM)ndc85430 Wrote: [ -> ]Why have the class in the first place? Those two functions look like they're entirely separate and standalone. The class just adds unnecessary complexity.
yeah, completely agree and also noted it in a previous post, but OP claims it's a skeleton code given to them. Poor design.
Traceback(most recent call last):
File "<string>",line 18, in <module>
File "<string>",line 14 in sumofarmstrongnumbers
TypeError:isarmstrong() missing 1 required positional argument: 'number'
Pages: 1 2