DEV Community

Subh
Subh

Posted on

What is wrong with my Python code ?

I am new to python and trying to use python for learning DSA. While trying this code it is not working properly.

test.addOne() should increase the num value but it is not .
and also the while loop is infinite

It will be great if somebody could help me. Thanks in advance.

class Test:
    def __init__(self):
        self.num = 1

    def addOne(self):
        if self.num >= 1 and self.num <= 10 :
            self.num+1
        else:
            self.num = 0

    def getNum(self):
        return self.num


test =Test()

while test.getNum() != 0:
    test.addOne()
    print( test.getNum() )

Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
su_subh1 profile image
Subh

sorry all , after 3 hours to troubleshooting , i got the error , it is
self.num+1 should be self.num += 1

Collapse
 
chrisgreening profile image
Chris Greening

Great job figuring it out! :D