# Math Problem Solving

This program takes the first number you input and divides by 2, then takes the result and multiplies by 4. I decided to use classes and file writing because I just learned it yesterday. Please critique.

# Write a program with two functions.
# The first function should take an integer as a parameter and return the result of the
# integer divided by 2.
# The second function should take an integer as a parameter and return the result of
# the integer multiplied by 4. Call the first function, save the result as a variable,
# and pass it as a parameter to the second function

myList = []

class Math:
def __init__(self, x):
self.number1 = x

#The try and excepts were created just in case I change the input to be mine instead of users's
#Divide the input

def divide(self):
try:
self.division = self.number1 // 2
return self.division
except (TypeError, NameError):
print("Unsupported format")

#Multiply the returned input

def multiply(self):
try:
self.multiplication = self.division * 4
myList.append(self.multiplication)
except (AttributeError, NameError):
print("First variable was not recognized")

while True:
try:
myNumber = Math(int(input("What number do you choose?"
"\nThis number will be divided by 2"
"\nThen it will be multiplied by 4\n")))
myNumber.divide()
myNumber.multiply()
print(myList)
tryAgain = input("Would you like to try again? "
"Press any key to continue. Press N to exit\n")

if tryAgain == "N".lower():
with open("data.txt", "w") as myData:
myData.write("My data is: " + str(myList))
break

except(NameError,ValueError):
print("Invalid Input")

• BTW, when asked "try again?", answering "N" will not cause the program to exit since you check for equality against "N".lower() which is "n". Either lower or upper both for the wanted result. – bergerg Sep 3 '17 at 15:16

Your coding style could be improved, for instance you could respect the naming conventions detailed in the PEP8.

Using classes is not judicious (Python is not Java). You could use simple functions. I understand that this is an exercise but it looks weird.

A function can have parameters and return a value. For instance with divide, you can write:

def divide(number):
return number // 2


You can do the same for multiply. To use those functions:

number = ...
result = multiply(divide(number))


Catching TypeError, NameError and AttributeError is not a good practice because they are generally raised because the developer (you) had made an error. This is not the user fault, so the message you display to the user is not useful.

If you really want a class here, you can store the list of results in instead of the user input.

class Math(object):
def __init__(self):
self.results = []

def divide(self, number):
return number // 2

def multiply(self, number):
return number * 4

def calc(self, number):
result = self.multiply(self.divide(number))
self.results.append(result)


You can use this class like this:

my_math = Math()
my_math.calc(5)
my_math.calc(8)
print(my_math.results)
# -> [8, 16]


I'm repeating myself: divide and multiply should be functions, not methods here.

• Okay, thank you! I have a question though. I put in try and except because when I would intentionally put in a string, or undefined variable in my methods, it would give me that error. I did that on purpose, so is there a better practice for this? Thank you again, for critiquing my code. I'm very new to this. I just learned classes and file writing yesterday. So I wanted to try throwing everything together. – thesolidmoose Sep 3 '17 at 16:00
• "undefined variable", humm! There no such a thing in Python. NameError is raised when a local or global name is not found. In Python, a variable can't be "declared" and "undefined". – Laurent LAPORTE Sep 3 '17 at 17:27
• That's what I meant, please excuse my ignorance. May I ask what career path you decided to go down with programming? What do you do daily? I have researched this question, and gotten answers, but I would like to directly ask the individual who is assisting me. Thank you! – thesolidmoose Sep 4 '17 at 16:12
• Do you need more explanation? I suggest you to upvote and accept my answer. – Laurent LAPORTE Sep 4 '17 at 21:45
• So may I now ask more questions? – thesolidmoose Sep 6 '17 at 2:25