Python Forum
changing value and rerunning the code - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: changing value and rerunning the code (/thread-7631.html)



changing value and rerunning the code - Arifattal - Jan-18-2018

i need this for something else i am working on, so i added it into a more simple code,
what i am trying to do is tell the code that if self.w(the third number) is greater than 20, self.z(the second number) will equal 0(in case the user wants that).
now i want the code to run again with the value the user previously put in, and with self.z = o.
class First():
    def number_input(self):
        self.x = int(input("whats your first number?\n"))
        print('your first number is', self.x)

class Second():
    def result(self):
           self.z = self.x + 2
           print('your second number is', self.z)

class Third():
    def last_result(self):
        self.w = self.z + self.x
        print('your third number is',self.w)

class if_over(First, Second, Third):
    def over(self):
        if self.w > 20:
            yes_no = (input("do you want your second number to equal 0?\n"))
            if yes_no == 'yes':
                self.z = 0





obj = if_over()
obj.number_input()
obj.result()
obj.last_result()
obj.over()



RE: changing value and rerunning the code - Larz60+ - Jan-18-2018

No need for class here.


RE: changing value and rerunning the code - Arifattal - Jan-18-2018

(Jan-18-2018, 04:40 PM)Larz60+ Wrote: No need for class here.
i need it for something else in which i am using classes, is it not possible?


RE: changing value and rerunning the code - nilamo - Jan-18-2018

Instead of if_over inheriting from all three other classes, it should only inherit from Third, which in turn extends Second, which then extends First.

The reasoning being that Second uses self.x, which is only defined in First, and Third uses self.z, which is only defined in Second. So those classes should extend from what they need, so they can be used by themselves. Your code, as it is, will never work if you try to create an object of any class EXCEPT if_over. Ideally, each piece of the chain will work, and the next piece adds more, instead of the bottom piece being the only usable one.


RE: changing value and rerunning the code - Arifattal - Jan-19-2018

(Jan-18-2018, 07:32 PM)nilamo Wrote: Instead of if_over inheriting from all three other classes, it should only inherit from Third, which in turn extends Second, which then extends First.

The reasoning being that Second uses self.x, which is only defined in First, and Third uses self.z, which is only defined in Second. So those classes should extend from what they need, so they can be used by themselves. Your code, as it is, will never work if you try to create an object of any class EXCEPT if_over. Ideally, each piece of the chain will work, and the next piece adds more, instead of the bottom piece being the only usable one.

thanks for the tip brother,the explanation makes so much sense.
any suggestion to how i may solve this issue?


RE: changing value and rerunning the code - nilamo - Jan-19-2018

class First:
    pass

class Second(First):
    pass

class Third(Second):
    pass

class if_over(Third):
    pass