Apr-07-2017, 08:33 PM
(This post was last modified: Apr-07-2017, 08:33 PM by teenspirit.)
I have an update on my program. I am going to make a function be the main menu. My current code is like this:
here:
#! /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 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, 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: ") 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] = 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': main() 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 'm' to go back to the main function. Your data will be saved.") #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 ('m', 'main'): break else: print('......An incorrect or blank character has been entered, try the last entry again.') print() return large, small main() #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) 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)) #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 + '.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') def main(): 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: ") while userin != 'e': if userin == 'i': food_items() elif userin == 'r': one_or_two() elif userin == 't': large, small = one_or_two() total(large, small) elif userin == 'e': large, small = one_or_two() total(large, small) break else: print('An unknown option: ', userin, ' has been entered. Please try the last entry again.') main()My problem is this: how can i return to the main menu while saving the data already in the functions? I need to be able to switch between registering and items, just in case, so i need to make a function that can go back and forth between them. obviously this code doesnt work, or i wouldnt be asking for more help.
here:
#this function defines the first input data and how to store that data def one_or_two(): # instruction for quitting print("Press 'm' to go back to the main function. Your data will be saved.") #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 ('m', 'main'): break else: print('......An incorrect or blank character has been entered, try the last entry again.') print() return large, small main()It simply restarts the current function, losing the data that was previously entered. I need it to finish the function, return or store the values somehow, then return to the main menu. I need the food_items(), total(), and one_or_two() functions to be able to use this solution. Thanks again to the fantastic people who have helped me on this project, i am learning quite a bit about making larger projects with python.