jseiser
January 3rd, 2009, 06:04 AM
I learned to program using .net languages in school. I am trying to learn python and I am just writing simple little things to try and learn. I just want to know how I can better call my functions, or better yet, actually use classes.
here is my code for a simple script that asks for 2 numbers, and then displays them added, subtracted, multiplied and divided. its nice and simple, but I would much rather have it call my functions properly, I feel like I have them nested and that it isnt the proper way to do this.
I call the numbers() first, which then calls the quit function, which then calls the triggers function which calls all the other functions then prints out the answers. It seems like you would actually want to just call
if __name__ == "__main__":
numbers()
quit()
trigger()
add()
subtract()
multiply()
divide()
printstr()
I guess I am just looking for input on how a python programmer would go about writing this. Am I making to much work out of it with all the functions or should I try writing in a object oriented way and use classes? Classes are something I struggled with in school :(
#!/usr/bin/env python
#
# Programming Challenge simple calculator
#
import sys
#!/usr/bin/env python
#
# Programming Challenge #5 (game)
#
import sys
def add(a,b):
""" Adds 2 numbers that are passed to it"""
added = (a + b)
return added
def subtract(a,b):
""" Subtracts 2 numbers that are passed to it """
subtracted = (a - b)
return subtracted
def multiply(a,b):
""" multiplies 2 numbers that are passed to it"""
multiplied = (a * b)
return multiplied
def divide(a,b):
""" divides 2 numbers that are passed to it"""
divided = (a / b)
return divided
def numbers():
"""gets to numbers from the user"""
print ("Type quit to exit")
num1 = raw_input(" Please give me a number: ")
quit(num1)
num2 = raw_input(" Please give me a number: ")
quit(num2)
num1 = int(num1)
num2 = int(num2)
trigger(num1,num2)
def trigger(num1,num2):
tempadd = add(num1,num2)
tempsubtract = subtract(num1,num2)
tempmultiply = multiply(num1,num2)
tempdivide = divide(num1,num2)
print " *********************************** \n"
print str(num1) + " + " + str(num2) + " = " + str(tempadd)
print str(num1) + " - " + str(num2) + " = " + str(tempsubtract)
print str(num1) + " * " + str(num2) + " = " + str(tempmultiply)
print str(num1) + " / " + str(num2) + " = " + str(tempdivide)
def quit(test):
if test == "quit":
sys.exit()
if __name__ == "__main__":
numbers()
def add(a,b):
""" Adds 2 numbers that are passed to it"""
added = (a + b)
return added
def subtract(a,b):
""" Subtracts 2 numbers that are passed to it """
subtracted = (a - b)
return subtracted
def multiply(a,b):
""" multiplies 2 numbers that are passed to it"""
multiplied = (a * b)
return multiplied
def divide(a,b):
""" divides 2 numbers that are passed to it"""
divided = (a / b)
return divided
def numbers():
"""gets to numbers from the user"""
print ("Type quit to exit")
num1 = raw_input(" Please give me a number: ")
quit(num1)
num2 = raw_input(" Please give me a number: ")
quit(num2)
num1 = int(num1)
num2 = int(num2)
trigger(num1,num2)
def trigger(num1,num2):
tempadd = add(num1,num2)
tempsubtract = subtract(num1,num2)
tempmultiply = multiply(num1,num2)
tempdivide = divide(num1,num2)
print " *********************************** \n"
print str(num1) + " + " + str(num2) + " = " + str(tempadd)
print str(num1) + " - " + str(num2) + " = " + str(tempsubtract)
print str(num1) + " * " + str(num2) + " = " + str(tempmultiply)
print str(num1) + " / " + str(num2) + " = " + str(tempdivide)
def quit(test):
if test == "quit":
sys.exit()
if __name__ == "__main__":
numbers()
here is my code for a simple script that asks for 2 numbers, and then displays them added, subtracted, multiplied and divided. its nice and simple, but I would much rather have it call my functions properly, I feel like I have them nested and that it isnt the proper way to do this.
I call the numbers() first, which then calls the quit function, which then calls the triggers function which calls all the other functions then prints out the answers. It seems like you would actually want to just call
if __name__ == "__main__":
numbers()
quit()
trigger()
add()
subtract()
multiply()
divide()
printstr()
I guess I am just looking for input on how a python programmer would go about writing this. Am I making to much work out of it with all the functions or should I try writing in a object oriented way and use classes? Classes are something I struggled with in school :(
#!/usr/bin/env python
#
# Programming Challenge simple calculator
#
import sys
#!/usr/bin/env python
#
# Programming Challenge #5 (game)
#
import sys
def add(a,b):
""" Adds 2 numbers that are passed to it"""
added = (a + b)
return added
def subtract(a,b):
""" Subtracts 2 numbers that are passed to it """
subtracted = (a - b)
return subtracted
def multiply(a,b):
""" multiplies 2 numbers that are passed to it"""
multiplied = (a * b)
return multiplied
def divide(a,b):
""" divides 2 numbers that are passed to it"""
divided = (a / b)
return divided
def numbers():
"""gets to numbers from the user"""
print ("Type quit to exit")
num1 = raw_input(" Please give me a number: ")
quit(num1)
num2 = raw_input(" Please give me a number: ")
quit(num2)
num1 = int(num1)
num2 = int(num2)
trigger(num1,num2)
def trigger(num1,num2):
tempadd = add(num1,num2)
tempsubtract = subtract(num1,num2)
tempmultiply = multiply(num1,num2)
tempdivide = divide(num1,num2)
print " *********************************** \n"
print str(num1) + " + " + str(num2) + " = " + str(tempadd)
print str(num1) + " - " + str(num2) + " = " + str(tempsubtract)
print str(num1) + " * " + str(num2) + " = " + str(tempmultiply)
print str(num1) + " / " + str(num2) + " = " + str(tempdivide)
def quit(test):
if test == "quit":
sys.exit()
if __name__ == "__main__":
numbers()
def add(a,b):
""" Adds 2 numbers that are passed to it"""
added = (a + b)
return added
def subtract(a,b):
""" Subtracts 2 numbers that are passed to it """
subtracted = (a - b)
return subtracted
def multiply(a,b):
""" multiplies 2 numbers that are passed to it"""
multiplied = (a * b)
return multiplied
def divide(a,b):
""" divides 2 numbers that are passed to it"""
divided = (a / b)
return divided
def numbers():
"""gets to numbers from the user"""
print ("Type quit to exit")
num1 = raw_input(" Please give me a number: ")
quit(num1)
num2 = raw_input(" Please give me a number: ")
quit(num2)
num1 = int(num1)
num2 = int(num2)
trigger(num1,num2)
def trigger(num1,num2):
tempadd = add(num1,num2)
tempsubtract = subtract(num1,num2)
tempmultiply = multiply(num1,num2)
tempdivide = divide(num1,num2)
print " *********************************** \n"
print str(num1) + " + " + str(num2) + " = " + str(tempadd)
print str(num1) + " - " + str(num2) + " = " + str(tempsubtract)
print str(num1) + " * " + str(num2) + " = " + str(tempmultiply)
print str(num1) + " / " + str(num2) + " = " + str(tempdivide)
def quit(test):
if test == "quit":
sys.exit()
if __name__ == "__main__":
numbers()