Sign in to follow this  
orbikk

Python programming, help :]

Recommended Posts

Hey everyone, I'm a beginner at programming and heard python is the best to start out with to learn the basics. I have read tutorials online and have learned a good bit. I am interested in making a program to automates breaks for people at work.

for example. I want the program to take the names entered, and the hours they are working, i want it to first do the sort() to sort the hours numerically in order than take than take those and sort() the names of the people alphabetically so for example it would output like this..

Andy 8 - 4
Billy 8 - 4


Anyhow, I have the menu coded which was very simple to do.. This is the code I have so far. not very far sorry :\ I put in comments where I need help at, thanks :]

__________________________________________________________________________

def menu():
print "Welcome to breaksheet V1!"
print ""
print "What would you like to do?"
print ""
print "1) Create New Break Sheet"
print "2) Quit"
print ""
return input ("Choose an option: ")

phonebook = {} ## <-- I created a blank phonebook so whenever they create a name it gets put into the phonebook?

def create(a,b): ## <--- Here is the create option, I'm not sure which code I should use to actually make it when they type in the name it gets put into the phone book


loop = 1
choice = 0

while loop == 1:
choice = menu()
if choice == 1:
print " "
create(input("First Name: "),input("Last Name: "))

[Edited by - orbikk on October 13, 2010 4:21:51 PM]

Share this post


Link to post
Share on other sites
So I need to know how to add in a code for whenever someone types in the workers name it puts it into the phonebook.

Here is what im looking for..


- Collect the names of the people that will be working
- Collect the hours they will be working
- Take the people with the earliest hours
- Put them into alphabetical order
- Give each person a 15 minute break, a 30 minute lunch, and an additional 15 minute break
- Do not allow more than 2 people have break at the same time
- Only people working 6 hours + gets a 30 minute lunch break


[Edited by - orbikk on October 13, 2010 4:59:24 PM]

Share this post


Link to post
Share on other sites
In python, adding to a dictionary collection is as simple as:


phonebook = {}
phonebook['Andy'] = '555-123456'
phonebook['Billy'] = '555-999999'
print( phoneBook['Billy'] )
print( phoneBook )




However in your case a dictionary might not be the most appropriate choice, as it is geared towards performing a lookup based on a unique "key", rather than just storing a list. It might be useful if you were storing phone numbers or other information and wanted to be able to look them up based on a name, but right now you are only dealing with first and last names.

A more appropriate choice here might be a list containing several tuples - a single tuple is like a variable containing several pieces of information, packed together in a list in a fixed order. For example, a tuple representing a worker and their hours:


## Using 24 hr times as it will make sorting etc. a bunch easier later on
some_tuple = (8,16,'Smith','Andy') # Andy Smith works 8am to 4pm




A list object is similar to your phonebook dictionary object, but isn't concerned with looking up a particular entry based on a key - it does, however, allow you to sort the items in it, add items to the end or anywhere in the middle, remove items etc. For example:


# Create the empty list
workerList = []

# Add (to the end of the list, so "append") some tuples
workerList.append( (8,16,'Smith','Billy') )
workerList.append( (14,22,'Aaronson','Aaron') ) ## late shift worker
workerList.append( (8,16,'Smith','Andy') )

workerList.sort()
print (workerList)




As you can see, this has done most of the work for you and sorted the list in order of working hours, followed by name.

A couple of things to consider later on:
  • You might create a dictionary as well as a list if you were storing contact details, to allow you to quickly look these details up

  • You may reach a point where your tuples get long and it becomes difficult to remember the order of information in them - once you start to learn about classes you might want to use them instead to store your employee information. One of the strengths of tuples in python is the ease with which you can create one and that doing things like sorting lists of them works nicely "out of the box". Classes require a bit more work initially, allow you to organize your data in a more "formal" way - referring to the "lastName" or "startHour" of an employee explicitly rather than implicitely based on their position in a tuple

    Share this post


    Link to post
    Share on other sites
    Say I want the program to run, and it's going to ask for the list of names of people that are working that day.. So you type the names in, and they automatically get placed into the list of names, then it will come up with what time will "so and so" be arriving, and than it will ask how many hours will they be working and you put that in.

    I need a program where it ask for information and you type it in, not so much a program where I put all the information in.

    Share this post


    Link to post
    Share on other sites
    Quote:
    Original post by orbikk
    Say I want the program to run, and it's going to ask for the list of names of people that are working that day.. So you type the names in, and they automatically get placed into the list of names, then it will come up with what time will "so and so" be arriving, and than it will ask how many hours will they be working and you put that in.

    I need a program where it ask for information and you type it in, not so much a program where I put all the information in.


    Ok, I understand that - you're actually almost there already and by combining what you've got with what I've given you, you should have enough to get this working.

    Consider this example, it should give you some ideas about how to go about this:


    employeeNames = []

    gettingNames = True

    while gettingNames == True:
    firstName = input("Enter first name: ")
    lastName = input("Enter last name: ")

    employeeNames.append( (lastName, firstName) ) ## Add to list

    enterAnother = input("Add another employee? (y/n)")
    if enterAnother.lower() == "n": ## lower converts to lower case
    gettingNames = False


    employeeDetails = []

    for name in employeeNames:
    print ("Employee: " + name[1] + " " + name[0])

    shiftStart = input("Start hour: ")
    shiftDuration = input("Shift duration: ")

    employeeDetails.append( (shiftStart, shiftDuration, name[0], name[1]) )

    employeeDetails.sort()
    print (employeeDetails)


    Share this post


    Link to post
    Share on other sites
    Hey, I tried putting that code into there and when I run the program, it says "Enter First Name:" and I enter Justin, and it gives me this error.


    Traceback (most recent call last):
    File "C:\Python24\program", line 16, in -toplevel-
    firstName = input("Enter first name: ")
    File "<string>", line 0, in -toplevel-
    NameError: name 'Justin' is not defined


    this is the code im using



    def menu():
    print "Welcome to breaksheet V1!"
    print ""
    print "What would you like to do?"
    print ""
    print "1) Create New Break Sheet"
    print "2) Quit"
    print ""
    return input ("Choose an option: ")

    employeeNames = []

    gettingNames = True

    while gettingNames == True:
    firstName = input("Enter first name: ")
    lastName = input("Enter last name: ")

    employeeNames.append( (lastName, firstName) ) ## Add to list

    enterAnother = input("Add another employee? (y/n)")
    if enterAnother.lower() == "n": ## lower converts to lower case
    gettingNames = False


    employeeDetails = []

    for name in employeeNames:
    print ("Employee: " + name[1] + " " + name[0])

    shiftStart = input("Start hour: ")
    shiftDuration = input("Shift duration: ")

    employeeDetails.append( (shiftStart, shiftDuration, name[0], name[1]) )

    employeeDetails.sort()
    print (employeeDetails)

    Share this post


    Link to post
    Share on other sites
    Use raw_input instead of input. input takes the input and evaluates it as Python code (so in other words, 'Justin' looks like a variable to Python). raw_input simply takes the input and dumps it into a string.

    Share this post


    Link to post
    Share on other sites
    Hello orbikk, I took the liberty of cleaning it a little bit. Could you please tell us the Python version you are coding in?(2.x or 3.x?)

    Now I will tell you what I changed:
    • Instead of using a loop with a conditional variable I created a wild loop with a 'break' statement if the input == 'n'

    • You don't need to take inputs separately or even outside of commands.

    • At the end I guessed you meant that.


    employeeNames = []
    while 1:
    employeeNames.append((raw_input("Enter last name: "), raw_input("Enter first name: "))) # Totally vaild.
    if str(raw_input("Add another employee? (y/n)")).lower() == 'n': break # Break will "break" out of the loop(while or for) it is inside of.

    employeeDetails = []
    for name in employeeNames:
    print "Employee:", name[1], name[0]
    employeeDetails.append((raw_input("Start hour: "), raw_input("Shift duration: "), name[0], name[1])) #Again all into a line and without having to store values twice.

    employeeDetails.sort()
    for detail in employeeDetails:
    print "Employee:", str(detail[2]), str(detail[3]), ";Start hour:", str(detail[0]), ";Shift:", str(detail[1]) # Did you meant this?





    I hope it helps.

    EDIT
    Quote:

    Use raw_input instead of input. input takes the input and evaluates it as Python code (so in other words, 'Justin' looks like a variable to Python). raw_input simply takes the input and dumps it into a string.


    As he said. Use raw_input() for input.
    EDIT
    Finally got the formatting right. :D

    [Edited by - fabianhjr on October 15, 2010 7:10:09 AM]

    Share this post


    Link to post
    Share on other sites
    Hey, thanks for the reply! Your codes are really great and work well! Your code does exactly what I want it to, I want it to sort by names, and hours. Although, I want it to sort by the time first, than sort by the name. The main thing I want the code to do is choose "three" different breaks for each person. For example..

    If John is working from 8 - 4
    His 1st (15 Minute) break would be at 10:15
    His 2nd (30 Minute) break would be at 12:00
    His 3rd (15 Minute) break would be at 2:30

    Although only people working 6+ hours can have a 30 minute lunch break.
    Also, no more than 2 people can have a break at the same time.

    Share this post


    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

    Sign in to follow this