Python Forum
Everything works except for one line of code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Everything works except for one line of code
#1
I'm learning classes in Python and I've written a program that outputs the value of two integer variables. And I want to include a method that sums them. Everything works (the program successfully outputs the value of x and the value of y okay. But in the final print statement where I'm saying where I want to output the text and then call the function (in the print statement) to add the two together is where it's going wrong.

I'm not getting any specific error messages other than a "not listening" message.

Here is my program:
class SumTheIntegers:
    def __init__(self):
        self.x = 10
        self.y = 13

def sum_integers():
    sum = x + y
    return sum
    
def main():
    SumIntegers = SumTheIntegers()
    
    print("The value of x is", SumIntegers.x)
    print("The value of y is", SumIntegers.y)
    print()
    print("The sum is", SumIntegers.sum_integers())
    
main()
Reply
#2
You need to look a little bit more into python oop

class SumTheIntegers:
    def __init__(self, x, y):
        self.x = x
        self.y = y
 
    def _sum(self):
        return self.x + self.y
     
def main():
    sum_integers = SumTheIntegers(10, 13)
     
    print("The value of x is", sum_integers.x)
    print("The value of y is", sum_integers.y)
    print()
    print("The sum is", sum_integers._sum())
     
main()
Reply
#3
  • def sum_integers(): should be indented into the class and should have self as a first parameter.
  • sum = x + y x &y need self. in front of them

class SumTheIntegers:
    def __init__(self):
        self.x = 10
        self.y = 13
 
    def sum_integers(self):
        sum = self.x + self.y
        return sum
     
def main():
    sum_integers = SumTheIntegers()
     
    print("The value of x is", sum_integers.x)
    print("The value of y is", sum_integers.y)
    print()
    print("The sum is", sum_integers.sum_integers())
     
main()
Reply
#4
(Sep-06-2019, 05:57 PM)ThomasL Wrote: You need to look a little bit more into python oop

class SumTheIntegers:
    def __init__(self, x, y):
        self.x = x
        self.y = y
 
    def _sum(self):
        return self.x + self.y
     
def main():
    sum_integers = SumTheIntegers(10, 13)
     
    print("The value of x is", sum_integers.x)
    print("The value of y is", sum_integers.y)
    print()
    print("The sum is", sum_integers._sum())
     
main()

You are totally not helpful in your reply. That's like someone asking you on the road to help you with their flat tire and you tell them "You need to learn a little more about tires". And then you drive away.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Unable to understand the meaning of the line of code. jahuja73 0 309 Jan-23-2024, 05:09 AM
Last Post: jahuja73
  My code works on Jupyter Lab/Notebook, but NOT on Visual Code Editor jst 4 1,045 Nov-15-2023, 06:56 PM
Last Post: jst
  Code works but doesn't give the right results colin_dent 2 722 Jun-22-2023, 06:04 PM
Last Post: jefsummers
  Code used to work 100%, now sometimes works! muzicman0 5 1,447 Jan-13-2023, 05:09 PM
Last Post: muzicman0
  Trying to loop through code to plot seaborn line plots across multiple subplots eyavuz21 0 1,685 Dec-05-2022, 10:46 AM
Last Post: eyavuz21
  Pandas - error when running Pycharm, but works on cmd line zxcv101 1 1,370 Jun-18-2022, 01:09 PM
Last Post: snippsat
  Python code to read second line from CSV files and create a master CSV file sh1704 1 2,409 Feb-13-2022, 07:13 PM
Last Post: menator01
  Pyspark - my code works but I want to make it better Kevin 1 1,797 Dec-01-2021, 05:04 AM
Last Post: Kevin
Question email code works in 2.7 but not in 3 micksulley 3 2,593 Nov-04-2021, 09:44 PM
Last Post: micksulley
  My simple code don't works !! Nabi666 1 1,617 Sep-06-2021, 12:10 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020