Python Forum
Help on a program to help with distributing food to the needy
Thread Rating:
  • 3 Vote(s) - 2.33 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help on a program to help with distributing food to the needy
#11
Ok, I have been advised to make what I need to do clearer. What I need to do is general predictions on the numbers of items to give to each family. The most important factors are:
How many of each item to give to 1's
How many of each item to give to 2's
How many people are we expecting total
What is the ratio in that total group of 1's to 2's

We have a set number of units for each item, they arrive on a truck with sheets saying how much we have of each. The main issue is that the ratio of 1's to 2's isn't always constant through and entire line, and we don't know exactly how many people will show up every time. I need to process the data, live, so that each booth knows how much to be giving out every 15-30 minutes or so. I want to make a list of items with their units and amount of units to be entered when trucks arrive, then take how much we have of everything, process it through an average of three or so runs so we have a general idea of what we are looking at for each item in terms of how many units to give out, then leave a little excess based off the predictions in case more 2's or families total show up than we expected, but distribute any of the unused surplus evenly over 15-30 minutes. The idea is to distribute the food evenly as possible over the whole group of families, but not run out or have excessive surplus's at the end. I hope that helps, if any area is unclear i will clear it up as much as possible.

Minor update to the code:
#for use in the total() function
from fractions import Fraction

def food_items():
   item = 0
   items = {}
   while True:
       item_name = input("Type the name of your food item here: ")
       item_units = input("Type the number of units of your item, just the number, here: ")
       items[item_name] = item_units
       next_step_option = input("Type 'a' to do another item, type 'n' to go to the next step': ")
       if next_step_option == 'a':
           print('Adding another food item')
       elif next_step_option == 'n':
           break
       else:
           print('......An incorrect or blank character has been entered, try the last entry again.')

#just a repepitive line
def show_sizes(large, small):
   print ('Large families: ', large, ' small families: ', small)
 
#this function defines the first input data and how to store that data
def one_or_two():
   # instruction for quitting
   print("Press 'q' to quit the program and get a final total.")
   #declareing the necessary variables
   large = 0
   small = 0
   while True:
       letter = input("Is it a large family or small? Type l for large family, s for small: ").lower()
       if letter in ('s', 'small'):
           #adding to the value of small families 
           small = small + 1
           show_sizes(large, small)
       elif letter in ('l', 'large'):
           #adding to the value of large families
           large = large + 1
           show_sizes(large, small)
       elif letter in ('q', 'quit'):
           break
       else:
           print('......An incorrect or blank character has been entered, try the last entry again.')
       print()
   return large, small
 
#this function gives a tally with information when the program is exited. I intend to make this also write to a file for better predictions in the future.
def total(large, small):
   final_total = large + small
   print('\n***There is a final total of ',final_total, ' families***')
   show_sizes(large, small)
   print('The ratio of large families to small families is: ' ,Fraction(large, small), ' large/small')
   print('{:.1%} of the families were large.'.format(large / final_total))
     
#simply calling the funtions we defined earlier
food_items()
large, small = one_or_two()
total(large, small)
What i did here was start to prepare a dictionary that would store all the total amounts of food, that i could then manipulate later on as i dispense the food. My next step would be to finish defining that function and comment it, add another function where you add the speculative ratio or percent of 1's to 2's, add the speculative total number of people, then make a final function or two that actually claculated and updated every 50 total entries how many of each item we should be handing out to each family. I truly appreciate the community help on this, but I am off to bed now, will start as soon as i can again tomorrow.
Reply
#12
You can't write a program to predict how many people will come, with particular needs. You can try but will not work. It is statistics. You have to have data for a long enough back in time in order to do some predictions.

Also, it's good to ask the people, do they know someone who has failed to come and how big is his/her family. You have to know how many people with needs live in the area. You can't predict anything without data. Are there people with health problems like diabetes or podagra for example who can't eat any food.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#13
(Apr-07-2017, 07:25 AM)wavic Wrote: You can't write a program to predict how many people will come, with particular needs. You can try but will not work. It is statistics. You have to have data for a long enough back in time in order to do some predictions. Also, it's good to ask the people, do they know someone who has failed to come and how big is his/her family. You have to know how many people with needs live in the area. You can't predict anything without data. Are there people with health problems like diabetes or podagra for example who can't eat any food.

The program is to help gather data sets that will make the predictions more accurate in the future. It will also handle the math of figuring out how much to give to each person. All these things were guessed without so much as using a calculator before, anything would be more accurate than the way we did it. The whole point is to try to get more accurate predictions by gathering data at the same time and taking the guesswork out of the hands of truck drivers.

The way it works is people show up starting at a certain time, begin registering an hour before the trucks arrive. Around 100-200 people are already registered when the trucks are ready to unload with product moved out and tables set up. We know exactly how much we have of each item we get, it's just that no one has ever really figured our average for 1's to 2's, or the average number of people who attend. The distribution portion, where people walk with their carts to each stand in the line and are given the certain amount of food lasts 2:30, after that we dont have any more people who show up and the food should be mostly finished. Computers are very good at looking at several averaged sets of data and making some projections and adjusting them as it goes, it is not uncommon to do things like this with python specifically and computers are great at it. We already have a registration table where people sign in to get their card, so implementation should not be much of an issue.
Reply
#14
A quick question, what OS are you using and what version of Python?
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#15
(Apr-07-2017, 12:56 PM)sparkz_alot Wrote: A quick question, what OS are you using and what version of Python?
I am using linux mint and i'm using version 3.5.2, stable for the dev tools im using (pluma and IDLE3)

I have made some more progress towards what i want to do. It will now be a lot more useful when gathering data from use, it writes to a time stamped file the results of the total() function, I intend to also write the contents of the items{} dictionary here, to get an idea of the amounts of foods i will be working with in the program. I still need to do some debugging on the food_items() function so that it can be resumed later and so that you can rewrite the value or name of specific items. I am almost ready to show the progress to Scott, the guy who coordinates the local food pantry.

#for use in the total() function
from fractions import Fraction
#to use in time and date stamps in the file written by the total() function
import time

def food_items():
    #This is the dictionary that will store our items and their respective amounts
    items = {}
    #This loop defines the user adding the items
    while True:
        #requests name and amount
        item_name = input("Type the name of your food item here: ")
        item_units = input("Type the number of units of your item, just the number, here: ")
        #writes the name and amount as the key and value in our dictionary
        items[item_name] = item_units
        #from here the user can enter a new item or go on to the registration step
        next_step_option = input("Type 'a' to do another item, type 'n' to go to the next step': ")
        if next_step_option == 'a':
            print('Adding another food item')
        elif next_step_option == 'n':
            break
        else:
            print('......An incorrect or blank character has been entered, try the last entry again.')

#just a repepitive line
def show_sizes(large, small):
    print ('Large families: ', large, ' small families: ', small)
  
#this function defines the first input data and how to store that data
def one_or_two():
    # instruction for quitting
    print("Press 'q' to quit the program and get a final total.")
    #declareing the necessary variables
    large = 0
    small = 0
    while True:
        letter = input("Is it a large family or small? Type l for large family, s for small: ").lower()
        if letter in ('s', 'small'):
            #adding to the value of small families 
            small = small + 1
            show_sizes(large, small)
        elif letter in ('l', 'large'):
            #adding to the value of large families
            large = large + 1
            show_sizes(large, small)
        elif letter in ('q', 'quit'):
            break
        else:
            print('......An incorrect or blank character has been entered, try the last entry again.')
        print()
    return large, small
  
#this function gives a tally with information when the program is exited and also writes it to a file.
def total(large, small):
    #we set local_time to equal the current time and date.
    timestr = time.strftime("%Y%m%d-%H%M")
    final_total = large + small
    #the following four lines print out a simple block of data.
    print('\n***There is a final total of ',final_total, ' families***')
    show_sizes(large, small)
    print('The ratio of large families to small families is: ' ,Fraction(large, small), ' large/small')
    print('{:.1%} of the families were large.'.format(large / final_total))
    #the next six lines make a file name that is time and date stamped and add the same data to it as was printed above.
    filename = 'food drive log'
    with open(filename + timestr, 'a') as file_object:
        file_object.write('***There is a final total of ' + str(final_total) + ' families***\n')
        file_object.write('the ratio of large families to small families is: ' + str(Fraction(large, small)) + ' large/small\n')
        file_object.write('{:.1%} of the families were large.\n'.format(large / final_total))
        file_object.write('Large families: ' + str(large) + ' Small families: ' + str(small) + '\n')
      
#simply calling the funtions we defined earlier
food_items()
large, small = one_or_two()
total(large, small)
Still needs a lot of work, but it is looking more and more like what i was hoping for in a final product. Does anyone know how to edit the code so that the lines stay in one place, so that it just edits the values rather than writing a whole new line? It is kind of annoying to have the lines scrolling across the screen the way they do right now.
Reply
#16
(Apr-07-2017, 01:13 PM)teenspirit Wrote: Does anyone know how to edit the code so that the lines stay in one place, so that it just edits the values rather than writing a whole new line? It is kind of annoying to have the lines scrolling across the screen the way they do right now.

Are you talking about pluma or IDLE3?
Reply
#17
Just some minor stuff, you say that you might want to share your program with other offices.  Unless you are certain they will all be using linux, keep in mind possible OS differences. An example:

Your line:
    with open(filename + timestr, 'a') as file_object:
might be:
    with open(filename + timestr + '.txt', 'a') as file_object:
The ".txt" suffix makes no difference to Linux, but makes it clickable in Windows.

Also, you might want to add to the very beginning of your file, a 'shebang' line:

#! /usr/bin/env python3
Many Linux and Windows systems have both Python 2 and Python 3 on them.  On Linux, the shebang line allows you to run your script by just typing "./file_name.py " and on Windows (with Python >= 3.4) the file can just be double clicked, the respective OS's will select the correct executable to run the file.

You are shadowing the variable names "large" and "small" inside and outside your functions, usually not a good idea.  Since it appears the only place they are used outside the scope of a function is here:

large, small = one_or_two()
total(large, small)
you might change them to "lrg" and "sml" or something similar.
Finally, with this line:
    print('The ratio of large families to small families is: ', Fraction(large, small), ' large/small')
should you ever have occasion to only have large families and no small families, you will raise a DivisionByZero error.

(Apr-07-2017, 01:13 PM)teenspirit Wrote: Does anyone know how to edit the code so that the lines stay in one place, so that it just edits the values rather than writing a whole new line?

If you mean what I think you mean, you could, at the top add "import os",
then add the following line (probably in your "def one_or_two" function

os.system('cls' if os.name == 'nt' else 'clear')
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#18
(Apr-07-2017, 02:13 PM)Kebap Wrote:
(Apr-07-2017, 01:13 PM)teenspirit Wrote: Does anyone know how to edit the code so that the lines stay in one place, so that it just edits the values rather than writing a whole new line? It is kind of annoying to have the lines scrolling across the screen the way they do right now.
Are you talking about pluma or IDLE3?
What i mean is when the program runs it gives you a prompt to enter a letter or value, when you hit enter it makes a second prompt, then a tthird, fourth, so on. i want one prompt that has the values listed, and instead of skipping to a new line every time, it would just change the values on the prompt.
Reply
#19
(Apr-07-2017, 04:37 PM)teenspirit Wrote: What i mean is when the program runs it gives you a prompt to enter a letter or value, when you hit enter it makes a second prompt, then a tthird, fourth, so on. i want one prompt that has the values listed, and instead of skipping to a new line every time, it would just change the values on the prompt.

You mean some sort of screen control/redrawing? For linux you can use traditional curses or maybe higher level/easier to use npyscreen (built on top of curses).

Using either one would make your program significantly more complicated. So I wonder if isnt better to start with something that ichabod801 suggested - maybe even spreadsheet with formula for counting cells with X or XX would work (and could contain explaining text/notes).
Reply
#20
(Apr-07-2017, 05:06 PM)zivoni Wrote:
(Apr-07-2017, 04:37 PM)teenspirit Wrote: What i mean is when the program runs it gives you a prompt to enter a letter or value, when you hit enter it makes a second prompt, then a tthird, fourth, so on. i want one prompt that has the values listed, and instead of skipping to a new line every time, it would just change the values on the prompt.
You mean some sort of screen control/redrawing? For linux you can use traditional curses or maybe higher level/easier to use npyscreen (built on top of curses). Using either one would make your program significantly more complicated. So I wonder if isnt better to start with something that ichabod801 suggested - maybe even spreadsheet with formula for counting cells with X or XX would work (and could contain explaining text/notes).

Ok, i thought that it might have been possible to do that easily, and no, spreadsheets and other forms of software really arent an option, they simple dont have the flexibility and processing power of what i am trying to do.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Optimization problem ( food menu ) Truman 3 3,567 Apr-01-2019, 09:28 PM
Last Post: Truman
  Packaging and Distributing App for Python 3.6 solaikannan 1 2,658 Aug-21-2017, 09:36 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020