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")
"N".lower()
which is"n"
. Either lower or upper both for the wanted result. \$\endgroup\$