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):
            self.division = self.number1 // 2
            return self.division
        except (TypeError, NameError):
            print("Unsupported format")

#Multiply the returned input

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

while True:
        myNumber = Math(int(input("What number do you choose?"
                              "\nThis number will be divided by 2"
                              "\nThen it will be multiplied by 4\n")))
        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))

        print("Invalid Input")
  • \$\begingroup\$ 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. \$\endgroup\$
    – bergerg
    Sep 3, 2017 at 15:16

1 Answer 1


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))

You can use this class like this:

my_math = Math()
# -> [8, 16]

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

  • \$\begingroup\$ 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. \$\endgroup\$ Sep 3, 2017 at 16:00
  • \$\begingroup\$ "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". \$\endgroup\$ Sep 3, 2017 at 17:27
  • \$\begingroup\$ 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! \$\endgroup\$ Sep 4, 2017 at 16:12
  • \$\begingroup\$ Do you need more explanation? I suggest you to upvote and accept my answer. \$\endgroup\$ Sep 4, 2017 at 21:45
  • \$\begingroup\$ So may I now ask more questions? \$\endgroup\$ Sep 6, 2017 at 2:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.