Python-programming-exercises icon indicating copy to clipboard operation
Python-programming-exercises copied to clipboard

Question #20, Level 3

Open usalie opened this issue 8 years ago • 10 comments

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!

usalie avatar Apr 06 '17 23:04 usalie

Don't you think that 100 is a typo?

I guess it should be for i in range(n): print (i)

AdityaSoni19031997 avatar Apr 07 '17 00:04 AdityaSoni19031997

I don't think it is a typo. Have you even tried to run your own suggested code?

usalie avatar Apr 07 '17 01:04 usalie

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

AdityaSoni19031997 avatar Apr 07 '17 01:04 AdityaSoni19031997

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)

AdityaSoni19031997 avatar Apr 07 '17 13:04 AdityaSoni19031997

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

hungrypanda avatar Aug 30 '17 04:08 hungrypanda

@hungrypanda edit it properly...

Sent from my Lenovo A6020a40 using FastHub

AdityaSoni19031997 avatar Aug 30 '17 07:08 AdityaSoni19031997

@AdityaSoni19031997 :+1:

hungrypanda avatar Aug 30 '17 09:08 hungrypanda

I think Q20 solution code wrong in for i in reverse(100): it is supposed to be : for i in putNumbers(100): print (i)

Nootas avatar May 23 '19 03:05 Nootas

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

choonhongyeoh0241 avatar Aug 14 '22 10:08 choonhongyeoh0241

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)

Taikamya avatar Aug 22 '22 09:08 Taikamya