Python Forum
Can you give me a code based on the given skeleton code? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Can you give me a code based on the given skeleton code? (/thread-25168.html)

Pages: 1 2


Can you give me a code based on the given skeleton code? - Bhavika - Mar-22-2020

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



RE: Can you give me a code based on the given skeleton code? - ndc85430 - Mar-22-2020

We aren't here to write code for you.


RE: Can you give me a code based on the given skeleton code? - buran - Mar-22-2020

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


RE: Can you give me a code based on the given skeleton code? - Bhavika - Mar-22-2020

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



RE: Can you give me a code based on the given skeleton code? - buran - Mar-22-2020

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


RE: Can you give me a code based on the given skeleton code? - Bhavika - Mar-22-2020

But even after doing that, there is an error.
TypeError: missing a positional value number


RE: Can you give me a code based on the given skeleton code? - buran - Mar-22-2020

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.


RE: Can you give me a code based on the given skeleton code? - ndc85430 - Mar-22-2020

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.


RE: Can you give me a code based on the given skeleton code? - buran - Mar-22-2020

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


RE: Can you give me a code based on the given skeleton code? - Bhavika - Mar-22-2020

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'