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.

You are making an uncontroled use of recursion: Get_num() calls Get_num(), etc. Each function must have a precise task to do, which can be described in a short documentation string. Look at the following pseudo code for example

# THIS IS PSEUDO CODE, NOT REAL CODE

def main_job():
    """Repeatedly ask a number, compute and display its factorial"""
    indefinitely:   # in python this is 'while True:'
        handle_one_number()
        if not want_another_one():
            display quit message
            return

def handle_one_number():
    """get a number from user, compute and display its factorial"""
    num = get_number()
    f = compute_factorial(num)
    display_result(num, f)

def get_number():
    """get an non negative integer from user"""
    indefinitely:
        answer = raw_input("please enter a number")
        try:
            num = int(answer)
        except ValueError:
            display error message
        else:
            if 0 > num:
                display error message
                continue
            else:
                return num

Also notice that function names are traditionaly not capitalized in python (it's not compulsory, however, read Pep 8 for a coding style guide)

Edited 4 Years Ago by Gribouillis

Thank you for the post. It will take me some time to our through your p-code and process it.

This article has been dead for over six months. Start a new discussion instead.