Skip to main content
deleted 127 characters in body; edited tags; edited title
Source Link
mdfst13
  • 22.4k
  • 6
  • 34
  • 70

Program asks Ask 2 terms from user and adds both ofadd them. Is there a shorter way to code the adding part?

I'm trying to makemaking a program that can add 2 terms that the user types. 

I think I've got identifying or evaluating the terms down. I used classes because I just recently learned it and I also heard it was good practice in order to make your programs shorter. But, when it comes to adding 2 terms together (which is the code under the elseelse and elifelif statements) I tried using classes but I have no idea how that would work, so I just sort of brute forced it. Is there a shorter or otherwise better way to write it downcode the adding part? Surely there is, and I would appreciate it if someone could show me what to learn or focus on or pretty much just any good advice so that I can code it cleaner. By the way I'm kinda new, been coding for 3 months now, well actually just 2 because my classes just started in August so I had to focus on school for a bit.

class TermEva:
    def __init__(self, Variable, Coefficient):
        self.Variable = Variable
        self.Coefficient = Coefficient
        self.ExVariable = Variable
        if self.Coefficient < 0:
            self.ExVariable = "-" + self.Variable

    def SimTerm(self):
        return "{}{}".format(self.Coefficient, self.Variable)

    def Expanded_Form(self):
        ExpandedForm = []
        for n in range(abs(self.Coefficient)):
            ExpandedForm.append(self.ExVariable)
        return ExpandedForm

Term1 = TermEva(input("Enter Variable 1: "), int(input("Enter its Coefficient: ")))
Term2 = TermEva(input("Enter Variable 2: "), int(input("Enter it's Coefficient: ")))

print(Term1.SimTerm())
print(Term2.SimTerm())

if Term1.Variable == Term2.Variable:
    VariableSum = Term1.Variable
    CoefficientSum = Term1.Coefficient + Term2.Coefficient
    ExVariableSum = VariableSum
    if CoefficientSum < 0:
        ExVariableSum =  "-" + VariableSum

    ExpandedFormSum = []
    for num in range(abs(CoefficientSum)):
        ExpandedFormSum.append(ExVariableSum)
    TermSum = str(CoefficientSum) + str(VariableSum)
elif Term1.Variable != Term2.Variable:
    ExTerm1_Variable = Term1.Variable
    ExTerm2_Variable = Term2.Variable
    if Term1.Coefficient < 0:
        ExTerm1_Variable = "-" + Term1.Variable
    if Term2.Coefficient < 0:
        ExTerm2_Variable = "-" + Term2.Variable
    ExpandedFormSum = []
    for num in range(abs(Term1.Coefficient)):
        ExpandedFormSum.append(ExTerm1_Variable)
    for num in range(abs(Term2.Coefficient)):
        ExpandedFormSum.append(ExTerm2_Variable)
    if Term2.Coefficient < 0:
        TermSum =(Term1.SimTerm() + Term2.SimTerm())
    elif Term2.Coefficient >= 0:
        TermSum =(Term1.SimTerm() + "+" + Term2.SimTerm())

print(TermSum)
```

Program asks 2 terms from user and adds both of them. Is there a shorter way to code the adding part?

I'm trying to make a program that can add 2 terms that the user types. I think I've got identifying or evaluating the terms down. I used classes because I just recently learned it and I also heard it was good practice in order to make your programs shorter. But, when it comes to adding 2 terms together (which is the code under the else and elif statements) I tried using classes but I have no idea how that would work, so I just sort of brute forced it. Is there a better way to write it down? Surely there is, and I would appreciate it if someone could show me what to learn or focus on or pretty much just any good advice so that I can code it cleaner. By the way I'm kinda new, been coding for 3 months now, well actually just 2 because my classes just started in August so I had to focus on school for a bit.

class TermEva:
    def __init__(self, Variable, Coefficient):
        self.Variable = Variable
        self.Coefficient = Coefficient
        self.ExVariable = Variable
        if self.Coefficient < 0:
            self.ExVariable = "-" + self.Variable

    def SimTerm(self):
        return "{}{}".format(self.Coefficient, self.Variable)

    def Expanded_Form(self):
        ExpandedForm = []
        for n in range(abs(self.Coefficient)):
            ExpandedForm.append(self.ExVariable)
        return ExpandedForm

Term1 = TermEva(input("Enter Variable 1: "), int(input("Enter its Coefficient: ")))
Term2 = TermEva(input("Enter Variable 2: "), int(input("Enter it's Coefficient: ")))

print(Term1.SimTerm())
print(Term2.SimTerm())

if Term1.Variable == Term2.Variable:
    VariableSum = Term1.Variable
    CoefficientSum = Term1.Coefficient + Term2.Coefficient
    ExVariableSum = VariableSum
    if CoefficientSum < 0:
        ExVariableSum =  "-" + VariableSum

    ExpandedFormSum = []
    for num in range(abs(CoefficientSum)):
        ExpandedFormSum.append(ExVariableSum)
    TermSum = str(CoefficientSum) + str(VariableSum)
elif Term1.Variable != Term2.Variable:
    ExTerm1_Variable = Term1.Variable
    ExTerm2_Variable = Term2.Variable
    if Term1.Coefficient < 0:
        ExTerm1_Variable = "-" + Term1.Variable
    if Term2.Coefficient < 0:
        ExTerm2_Variable = "-" + Term2.Variable
    ExpandedFormSum = []
    for num in range(abs(Term1.Coefficient)):
        ExpandedFormSum.append(ExTerm1_Variable)
    for num in range(abs(Term2.Coefficient)):
        ExpandedFormSum.append(ExTerm2_Variable)
    if Term2.Coefficient < 0:
        TermSum =(Term1.SimTerm() + Term2.SimTerm())
    elif Term2.Coefficient >= 0:
        TermSum =(Term1.SimTerm() + "+" + Term2.SimTerm())

print(TermSum)
```

Ask 2 terms from user and add them

I'm making a program that can add 2 terms that the user types. 

I think I've got identifying or evaluating the terms down. I used classes because I just recently learned it and I also heard it was good practice in order to make your programs shorter. But, when it comes to adding 2 terms together (which is the code under the else and elif statements) I tried using classes but I have no idea how that would work, so I just sort of brute forced it. Is there a shorter or otherwise better way to code the adding part? Surely there is, and I would appreciate it if someone could show me what to learn or focus on or pretty much just any good advice so that I can code it cleaner.

class TermEva:
    def __init__(self, Variable, Coefficient):
        self.Variable = Variable
        self.Coefficient = Coefficient
        self.ExVariable = Variable
        if self.Coefficient < 0:
            self.ExVariable = "-" + self.Variable

    def SimTerm(self):
        return "{}{}".format(self.Coefficient, self.Variable)

    def Expanded_Form(self):
        ExpandedForm = []
        for n in range(abs(self.Coefficient)):
            ExpandedForm.append(self.ExVariable)
        return ExpandedForm

Term1 = TermEva(input("Enter Variable 1: "), int(input("Enter its Coefficient: ")))
Term2 = TermEva(input("Enter Variable 2: "), int(input("Enter it's Coefficient: ")))

print(Term1.SimTerm())
print(Term2.SimTerm())

if Term1.Variable == Term2.Variable:
    VariableSum = Term1.Variable
    CoefficientSum = Term1.Coefficient + Term2.Coefficient
    ExVariableSum = VariableSum
    if CoefficientSum < 0:
        ExVariableSum =  "-" + VariableSum

    ExpandedFormSum = []
    for num in range(abs(CoefficientSum)):
        ExpandedFormSum.append(ExVariableSum)
    TermSum = str(CoefficientSum) + str(VariableSum)
elif Term1.Variable != Term2.Variable:
    ExTerm1_Variable = Term1.Variable
    ExTerm2_Variable = Term2.Variable
    if Term1.Coefficient < 0:
        ExTerm1_Variable = "-" + Term1.Variable
    if Term2.Coefficient < 0:
        ExTerm2_Variable = "-" + Term2.Variable
    ExpandedFormSum = []
    for num in range(abs(Term1.Coefficient)):
        ExpandedFormSum.append(ExTerm1_Variable)
    for num in range(abs(Term2.Coefficient)):
        ExpandedFormSum.append(ExTerm2_Variable)
    if Term2.Coefficient < 0:
        TermSum =(Term1.SimTerm() + Term2.SimTerm())
    elif Term2.Coefficient >= 0:
        TermSum =(Term1.SimTerm() + "+" + Term2.SimTerm())

print(TermSum)
Source Link

Program asks 2 terms from user and adds both of them. Is there a shorter way to code the adding part?

I'm trying to make a program that can add 2 terms that the user types. I think I've got identifying or evaluating the terms down. I used classes because I just recently learned it and I also heard it was good practice in order to make your programs shorter. But, when it comes to adding 2 terms together (which is the code under the else and elif statements) I tried using classes but I have no idea how that would work, so I just sort of brute forced it. Is there a better way to write it down? Surely there is, and I would appreciate it if someone could show me what to learn or focus on or pretty much just any good advice so that I can code it cleaner. By the way I'm kinda new, been coding for 3 months now, well actually just 2 because my classes just started in August so I had to focus on school for a bit.

class TermEva:
    def __init__(self, Variable, Coefficient):
        self.Variable = Variable
        self.Coefficient = Coefficient
        self.ExVariable = Variable
        if self.Coefficient < 0:
            self.ExVariable = "-" + self.Variable

    def SimTerm(self):
        return "{}{}".format(self.Coefficient, self.Variable)

    def Expanded_Form(self):
        ExpandedForm = []
        for n in range(abs(self.Coefficient)):
            ExpandedForm.append(self.ExVariable)
        return ExpandedForm

Term1 = TermEva(input("Enter Variable 1: "), int(input("Enter its Coefficient: ")))
Term2 = TermEva(input("Enter Variable 2: "), int(input("Enter it's Coefficient: ")))

print(Term1.SimTerm())
print(Term2.SimTerm())

if Term1.Variable == Term2.Variable:
    VariableSum = Term1.Variable
    CoefficientSum = Term1.Coefficient + Term2.Coefficient
    ExVariableSum = VariableSum
    if CoefficientSum < 0:
        ExVariableSum =  "-" + VariableSum

    ExpandedFormSum = []
    for num in range(abs(CoefficientSum)):
        ExpandedFormSum.append(ExVariableSum)
    TermSum = str(CoefficientSum) + str(VariableSum)
elif Term1.Variable != Term2.Variable:
    ExTerm1_Variable = Term1.Variable
    ExTerm2_Variable = Term2.Variable
    if Term1.Coefficient < 0:
        ExTerm1_Variable = "-" + Term1.Variable
    if Term2.Coefficient < 0:
        ExTerm2_Variable = "-" + Term2.Variable
    ExpandedFormSum = []
    for num in range(abs(Term1.Coefficient)):
        ExpandedFormSum.append(ExTerm1_Variable)
    for num in range(abs(Term2.Coefficient)):
        ExpandedFormSum.append(ExTerm2_Variable)
    if Term2.Coefficient < 0:
        TermSum =(Term1.SimTerm() + Term2.SimTerm())
    elif Term2.Coefficient >= 0:
        TermSum =(Term1.SimTerm() + "+" + Term2.SimTerm())

print(TermSum)
```