#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    1
    Rep Power
    0

    Need help with simple programming homework!!!


    This is the homework:

    Program Names: Assign12_First_Last.py
    Due Date: May 6, 2013
    Points: 50
    Input file: None
    Description: Your program will read three boy’s names into a list. Then it will read three girl’s names into another list. You will concatenate the two lists into a third list. You will sort this list and print it out.
    1. Save as : Assign12_First_Last.py
    2. I want to see several comments. The top should be: # Assignment 12 - First Last
    3. You might want to start with program sales_list.py on p. 300 in the book. We also downloaded this program in class.
    4. Change the global variable to be equal to 3. You might also want to make this a more descriptive variable name.
    5. Instead of the list “sales”, you will have two lists, one for boys and one for girls.
    6. Input the boy’s names with the same while loop. Then, create another while loop to enter the girl’s names.
    7. Concatenate the boys and girls into a third list.
    8. Sort this new list.
    9. Print out the new list. (You can just use the for loop in the sales_list.py program.)
    10. Test and save. I will not grade any program that does not run.

    She wants us to edit this program:
    # The NUM_DAYS constant holds the number of
    # days that we will gather sales data for.
    NUM_DAYS = 3

    def main():
    # Create a list to hold the sales
    # for each day.
    sales = [0] * NUM_DAYS

    # Create a variable to hold an index.
    index = 0

    print('Enter the sales for each day.')

    # Get the sales for each day.
    while index < NUM_DAYS:
    print('Day #', index + 1, ': ', sep='', end='')
    sales[index] = float(input())
    index += 1

    # Display the values entered.
    print('Here are the values you entered:')
    for value in sales:
    print(value)

    # Call the main function.
    main()


    I try editing it but it won't let me put in letters even though it's a float.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,703
    Rep Power
    480
    Code:
    #Treat names as text, not as numbers.
    #I'd ignore the step 3 advice and write a new program.
    #This program isn't hard, and you've asked
    #for advice when the steps are fed with a baby spoon.
    #The sort function might not be trivial.
    
    def sort(L):
        sort_back_end(L,0,len(L))
    
    def sort_back_end(L, i, j):
        if j < i + 2:
            return
        I, J = i, j
        A = L[i]
        j -= 1
        L[i], L[j] = L[j], A
        while i + 1 < j:
            if L[i] < A:
                i += 1
            else:
                j -= 1
                L[i], L[j] = L[j], L[i]
        j -= A < L[i]
        sort_back_end(L, I, j)
        sort_back_end(L, j, J)
    
    
    print('enter some boys names, 1 per line')
    buoys = []
    for i in 'abc':buoys.append(input())
    
    print('enter some girls names, 1 per line')
    gulls = []
    for i in 'abc':gulls.append(input())
    
    names = gulls + buoys
    sort(names)
    print(names)
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    428
    Rep Power
    66
    Or, just for the fun of it:

    Code:
    #!/usr/bin/env python3
    
    def get_names(sex):
        names = []
        for i in (1, 2, 3):
            names.append(input('Enter the name for the {0}th {1}: '.format(i, sex)))
        return names
    
    for group_of_humans in ('boys', 'girls'):
        vars()[group_of_humans] = get_names(group_of_humans[:-1])
    bunch = sorted(boys + girls)
    print(', '.join(bunch))
    My armada: openSUSE 13.1 (home desktop, home laptop), Crunchbang Linux 11 (mini laptop, work laptop), Android 4.2.1 (tablet)

IMN logo majestic logo threadwatch logo seochat tools logo