Posts: 23
Threads: 11
Joined: Mar 2020
Mar-22-2020, 08:09 AM
(This post was last modified: Mar-22-2020, 08:16 AM by buran.)
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
Posts: 1,823
Threads: 2
Joined: Apr 2017
We aren't here to write code for you.
Posts: 8,091
Threads: 154
Joined: Sep 2016
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
Posts: 23
Threads: 11
Joined: Mar 2020
Mar-22-2020, 10:20 AM
(This post was last modified: Mar-22-2020, 10:28 AM by buran.)
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))
Posts: 8,091
Threads: 154
Joined: Sep 2016
Mar-22-2020, 10:27 AM
(This post was last modified: Mar-22-2020, 10:27 AM by buran.)
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
Posts: 23
Threads: 11
Joined: Mar 2020
But even after doing that, there is an error.
TypeError: missing a positional value number
Posts: 8,091
Threads: 154
Joined: Sep 2016
Mar-22-2020, 10:45 AM
(This post was last modified: Mar-22-2020, 10:46 AM by buran.)
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.
Posts: 1,823
Threads: 2
Joined: Apr 2017
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.
Posts: 8,091
Threads: 154
Joined: Sep 2016
Mar-22-2020, 10:46 AM
(This post was last modified: Mar-22-2020, 10:47 AM by buran.)
(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.
Posts: 23
Threads: 11
Joined: Mar 2020
Mar-22-2020, 10:58 AM
(This post was last modified: Mar-22-2020, 10:58 AM by Bhavika.)
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'
|