Python Forum
Can you give me a code based on the given skeleton code?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can you give me a code based on the given skeleton code?
#1
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
Reply
#2
We aren't here to write code for you.
Reply
#3
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
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))
Reply
#5
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
But even after doing that, there is an error.
TypeError: missing a positional value number
Reply
#7
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#8
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.
Reply
#9
(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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#10
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'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python with Finance - i have no code but don't do it for me give me tips please Akainu 1 2,480 May-30-2019, 07:57 PM
Last Post: nilamo
  cant get the code to give me the position of the word axius23 2 3,151 Oct-27-2017, 10:07 AM
Last Post: gruntfutuk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020