Jump to content

this is my code that we have for my homework and i cant figure out how to fix my error message

 

# Imports
from os import getcwd, chdir, path, mkdir
#add import for randint function
#add import for stdev function from statistics library
 
 
def get_inputs():
    '''Gets username and how many integers to generate input by the
    user. Returns both to main.'''
 
    username = input('Enter username: ')
 
    how_many = -1
    while (how_many < 100) or (how_many > 1000):
        how_many = int(input('Enter a number from 100 to 1000: '))
 
    return username, how_many
 
 
def create_subdir(subdir):
    '''Create a subdirectory. Does not return a value.
    subdir: a string representation of the subdirectory path.'''
    #Hint: see 'navigating directories examples.py' in Week 7 folder
    
    # if the path to subdir doesn't exist
        #create the subdirectory
    # otherwise, print message that subdir already exists
 
 
def write_rands(how_many, lower, upper, filename):
    '''Write random integers to a file. Does not return a value.
    how_many: the number of integers to write
    lower: lower bound for randint
    upper: upper bound for randint
    filename: file integers are written to'''
    outfile = open(filename, 'w')  # opens file for write
 
    #Hint: see 'write numbers to file.py' in Week 7 folder
 
 
def read_rands(filename):
    '''Read random integers from a file into a list. Returns a list.
    filename: a string variable naming the file to read from'''
    intlst = []  # initalize as empty list
 
    #Hint: see 'sum numbers from a file.py' in Week 7 folder
 
    return intlst
 
 
def mean(intlst):
    '''Calculate and return the mean.
    intlst: a list of integers'''
    #initialize accumulator variable
 
    #iterate through each number in intlst
        #add it to accumulator variable
 
    #get length of intlst and convert to a float; then use
    #to calculate the average
    
    return average
 
 
def median(intlst):
    '''Calculate and return the median. Copies intlst to a new list
    so that it is not reordered by .sort()
    intlst: a list of integers'''
    # Create a new copy of intlst to avoid side effects
    # because .sort() mutates the list
    sortlst = intlst[:]
 
    #sort sortlst with the built-in list sort function
    
 
    # Get middle of list
    middle = len(sortlst) // 2  #integer division to get middle of list
 
    if len(sortlst) % 2 == 0:  # List has even number of elements
        # This puts two 'middle' elements into a new list
        middlelst = [sortlst[middle-1], sortlst[middle]]
 
        #add call to your mean() function to calculate
        #the average of middlelst
        
    else:  # List has odd number of elements
        calc_median = sortlst[middle]
 
    return calc_median
 
 
# Can't use the name range, that's a built-in function
def myrange(intlst):
    '''Calculate and return the range.
    intlst: a list of integers'''
    #get min of intlist by searching it for the smallest element
 
    #get max of intlist by searching for largest element
 
    #calculate range, which is the max element minus the min element
 
    return calc_range
    
 
def std_dev(intlst):
    '''Calculate and return the standard deviation of intlist
    using built-in python function stdev().
    intlst: a list of integers'''
    # Use stdev() to calculate standard deviation
 
    return calc_stdev
    
 
def print_results(calc_mean, calc_median, calc_range, calc_stdev):
    '''Prints the results. Does not return a value.
    calc_mean: the calculated mean
    calc_median: the calculated median
    calc_range: the calculated range
    calc_stdev: the calculated standard deviation'''
 
 
 
def main():
    # Initialize constants and variables
    filename = 'stats.txt'
    lower = 27
    upper = 53
 
    # Request inputs
    username, how_many = get_inputs()
 
    # Create the subdirectory
    #assign a string to subdir variable that represents a path name that is
    #the subdirectory to create. Hint: see 'navigating directories examples.py'
    #in Week 7 folder
    create_subdir(subdir)
 
    # Change to the created subdirectory (this must be in main, not a function)
    ### add one python statement to change the directory
    print('current directory', getcwd())
 
    # Write random integers to file
    write_rands(how_many, lower, upper, filename)
 
    # Read random integers and assign to variable intlst
    #add function call to read_rands
 
    #########################################################
    #
    # For testing mean, median, myrange, and std_dev
    # functions. Comment out after testing.
    #
    intlst = [1, 2, 3, 4]
    #########################################################
 
    # Calculations
    calc_mean = mean(intlst)
    calc_median = median(intlst)
    #add function call to myrange
    #add function call to std_dev
 
    # Output results
    print_results(calc_mean, calc_median, calc_range, calc_stdev)
 
 
main()
 
 
 
thank you for your help
Link to comment
https://linustechtips.com/topic/240043-python-help-with-subdirectorys/
Share on other sites

Link to post
Share on other sites

Please use code tags and state what your error message/problem is.

 

Is it to help you figure out how to implement create_subdir(subdir)? If so the comments are already telling you what to do, and you seem to have some example code you can reference.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×