Apr-08-2017, 03:44 AM
This is now my progress on the program:
I also made a new total(large, small) function removing the file writing from that function, adding that functionality to the new final_total(large, small) function. I followed some of the advice for minor changes that has been given to me, and cleaned up the commenting a little bit. If anyone has any further suggestions or would like to help with development, please, just let me know. I am going to add writing the contents of our items dictionary to the onscreen total function and to the final_total function to add to the data given and stored with those functions.
#! /usr/bin/env python3 #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 get_item_info(items): #Requests name and amount, amount in integer form. 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: ") #The next six lines makes sure that the user has enterred an integer in try: value = int(item_units) break except ValueError: print('The number of units you entered: ', item_units, ' is not a normal number, please try entering it again.') continue #Writes the name and amount as the key and value in our dictionary. items[item_name] = int(item_units) #This prints the current list of items to the screen. for item, amount in items.items(): print(item + ' ..... ' + str(amount)) #This function defines the process of adding new food items to the items dictionary. def food_items(items): #This loop defines the user adding the items. while True: get_item_info(items) #After running the get_item_info function, this code will determine what the user will do next. next_step_option = input("Type 'a' to do another item, type 'm' to go to the main menu': ") #This statement says that if the input is 'a', it goes back to the top of our loop and recalls get_item_info(items) if next_step_option == 'a': print('Adding another food item') #This line exits to the main menu by breaking the loop, finishing the function call. elif next_step_option == 'm': break else: print('......An incorrect or blank character has been entered, try the last entry again.') #Just a repepitive line that it isn't practical to keep writing over and over. 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(large, small): # instruction for quitting print("Press 'm' to go back to the main menu. Your data will be saved.") 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) #This exits back to the main menu by breaking the loop, finishing the function call. elif letter in ('m', 'main'): 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 called 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) if small != 0: print('The ratio of large families to small families is: ' ,Fraction(large, small), ' large/small') else: print('The ratio of large families to small families is: ', large, '/0 large/small') print('{:.1%} of the families were large.'.format(large / final_total)) def final_total(large, small): total(large, small) #The next six lines make a filename that is time/date stamped and adds the specified information filename = 'food drive log' with open(filename + timestr + '.txt', 'a') as file_object: file_object.write('***There is a final total of ' + str(final_total) + ' families***\n') if small != 0: file_object.write('The ratio of large families to small families is: ' + str(Fraction(large, small)) + ' large/small\n') else: file_object.write('The ratio of large families to small families is: ' + str(large) + '/0 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') #This function is the main menu and controller of the other functions we have defined. def main(): #Declaring the necessary variables and dictionary. large, small = 0, 0 items = {} while True: #The following is our main menu that the user will see. userin = input("Type 'i' to enter new items, 'r' to begin registering new families, or 't' to get currnet total and stats, e is to exit: ") if userin == 'i': food_items(items) elif userin == 'r': large, small = one_or_two(large, small) elif userin == 't': total(large, small) elif userin == 'e': final_total(large, small) break else: print('An unknown option: ', userin, ' has been entered. Please try the last entry again.') #Calling our main function to start running the primary protions of our program. main()As you can see i split the food_items(items) function into two seperate functions, so that it would properly exit to the main menu.
I also made a new total(large, small) function removing the file writing from that function, adding that functionality to the new final_total(large, small) function. I followed some of the advice for minor changes that has been given to me, and cleaned up the commenting a little bit. If anyone has any further suggestions or would like to help with development, please, just let me know. I am going to add writing the contents of our items dictionary to the onscreen total function and to the final_total function to add to the data given and stored with those functions.