Here is a piece of code I wrote as to a homework assignment
I would like the community's opinion on structure and style. This code gives the factorial of a user defined number(near as I could tell by the online math help.)
print "This is a problem taken from Daniweb."
print "The object is to write this from memory and then convert it to C"
print "This proram is called factorials"
print "I would rather see my code work in Python than write Psudo!"
print "Code written by Python4Psudo"
print "Have a nice day!"
def Factorial_calculator(num):
# makes instance of a list from 1 to the target "num + 1"starting at index2
num_list = range(1, num+1)
# initial instance of the iterating, multiplying int
factorx = 1
# iterator
for i in num_list:
factorx = factorx*i
# return statement to End_user
print "The factorial for | " + str(num) + " | is [" + str(factorx) + "]"
# nested recursion
print "Would you like to try another number?"
answr=raw_input("(y, n) >> ")
if answr == "y":
Get_num()
else:
print "Exiting"
return
def Get_num():
num = raw_input("Please enter a number to get its factorial >>: ")
#input assigns to num for later evaluation.
if num < 1:
print "0! is always 1"
Get_Num()
else:
Data_type_check(num)
return
def Data_type_check(num):
# exception handling
try:
num = int(num)
Factorial_calculator(num)
except ValueError:
print("Only unsigned intergers allowed")
Get_num()
return
Get_num() # Function call returns global(num) variable.