Question #20, Level 3
Solution code does not seem to work. After running the solution provided I get TypeError:argument to reversed() must be a sequence I have edited reverse() as reversed(). Haven't found a good solution how to make reversed() sequential. Please help. Thank you!
Don't you think that 100 is a typo?
I guess it should be
for i in range(n): print (i)
I don't think it is a typo. Have you even tried to run your own suggested code?
I would prefer to use list comprehension and do the same task.. Pythonic way.
On 07-Apr-2017 06:36, "usakg" [email protected] wrote:
I don't think it is a typo. Have you even tried to run your own suggested code?
— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/zhiwehu/Python-programming-exercises/issues/15#issuecomment-292385163, or mute the thread https://github.com/notifications/unsubscribe-auth/AVr0ph_zdri1wTCeCVt9H5OxXbWE3wfxks5rtYwxgaJpZM4M2S0- .
sk = int(input("Enter number: "))
for x in range(1, n+1):
if (x % sk) == 0:
print(x, "is divisible by", sk)
else:
print(x, "is not divisible by", sk)
Basic code for the problem:
def divby7(n):
i = 1
listn = []
while i < n:
if i%7 == 0:
listn.append(i)
i += 1
return listn
OR
def divby7(n):
list_num = []
for num in range(1,n+1):
if num%7 == 0:
list_num.append(num)
return list_num
OR by using list comprehensions
def divby7(n):
list_num = [x for x in range (1,n+1) if x%7 == 0]
return list_num
@AdityaSoni19031997 :+1:
I think Q20 solution code wrong in
for i in reverse(100):
it is supposed to be :
for i in putNumbers(100): print (i)
Given the question requires class, I think the answer should be like this, at least
class Diviby7: def init(self): self.input = int(input("Enter a value: ")) self.value = []
def generator(self):
for i in range (0, self.input + 1):
if not i%7:
self.value.append(i)
else:
pass
def printOut(self):
return self.value
if name == 'main': a = Diviby7() a.generator() print(a.printOut())
You're right, @choonhongyeoh0241 ! It requires a Class, but it also hints at using Yield.
I combined both, doing the following:
It does require a class, you're right. It also hints at using yield.
To combine both, I did the following:
class Diviby7:
def __init__(self) -> None:
self.input = int(input("Enter a value: "))
self.value = [i for i in range(self.input) if i%7 == 0]
def gen(self):
yield self.value
def __repr__(self):
return f"Value: {next(self.gen())!r}"
if __name__ == "__main__":
a = Diviby7()
print(a)