def main():
choice = 0
income = 4000
global maxincome
maxincome = 4000
outFile = open('bh.txt', 'w')
outFile = open('bh.txt', 'a')
print >> outFile, '**Detailed budget history:-'
print >> outFile
print >> outFile, 'Starting balance: ',maxincome
inFile = open('bh.txt', 'w')
inFile = open('bh.txt', 'a')
print >> inFile, '**Detailed budget history:-'
print >> inFile
print >> inFile, 'Starting balance: ',maxincome
userInput = raw_input('Welcome to the financial planner. Press enter to continue: ')
if userInput != "":
choice=int(userInput)
while choice <> 999:
choice = menu(choice)
if choice == 1:
income, amt = addExp(income)
outFile = open('bh.txt', 'a')
print >> outFile, 'Expense Added =',amt,'Available Income:',income
elif choice == 2:
income, amt = removeExp(income)
outFile = open('bh.txt', 'a')
print >> outFile,'Expense Removed =',amt,'Available Income:',income
elif choice == 3:
income, amt = addIncome(income)
outFile = open('bh.txt', 'a')
print >> outFile,'Income Added =',amt,'Availabe Income:',income
elif choice == 4:
income, amt = removeIncome(income)
outFile = open('bh.txt', 'a')
print >> outFile,'Income Removed =',amt,'Availabe Income:',income
elif choice == 5:
inFile = open('bh.txt', 'r')
displayBudgetHistory(income, inFile)
elif choice == 999:
print '**Thanks for using the budget utility...'
else: print '***Invalid selection'
outFile.close()
inFile.close()
def addExp(income):
print
amt = input('**Enter expense to add: ')
print
if (amt <= income and amt > 0):
print '**Funds before adding adding expense: ',income
income = income-amt
print '**Funds after adding expense: ', income
return (income, amt)
elif amt > income:
print '**Insufficient funds. Cant add this expense: ',income, 'Expense: ',amt
else: print '**Invalid entry'
def removeExp(income):
print
amt = input('**Enter expense amount to be removed: ')
print
global maxincome
if (amt + income <= maxincome):
print '**Funds before removing the expense: ',income
income = income + amt
print '**Funds after removing expense: ',income
elif ((amt + income) > maxincome):
print '**Unable to remove expense. Invalid expense: ',income,' < Expense: ',amt
else: print '**Invalid Expense'
return(income, amt)
def addIncome(income):
print
amt = input('**Enter income to add: ')
print
if (amt <= income and amt > 0):
print '**Funds before adding income: ',income
income = income + amt
print '**Funds after adding income: ',income
return (income, amt)
def removeIncome(income):
print
amt = input('**Enter income amount to be removed: ')
print
global maxincome
if (amt - income <= maxincome):
print '**Funds before removing the income: ',income
income = income - amt
print '**Funds after removing income: ',income
elif ((amt + income) > maxincome):
print '**Unable to remove income. Invalid income: ',income,' < Income: ',amt
else: print '**Invalid income'
return(income, amt)
def displayBudgetHistory(income, inFile):
print
text = inFile.read()
print text
def menu(choice):
print '-:MAIN MENU:-'; print;
print '1: <---Add Expense'
print '2: <---Remove Expense'
print '3: <---Add Income'
print '4: <---Remove Income'
print '5: <---Display Transaction History'; print;
print '999: <---Exit'; print;
choice = ""
while choice == "":
choice = raw_input('***Enter a selection: ')
return int(choice)
print
main()
I am able to run the program fine. The only problem is its not crash resistant. I have researched for three weeks trying to find an answer but have been unable to come up with one. Can anybody help me with this? I have already tried using the try: exception method, but I may have written it incorrectly. Any help would be greatly appreciated.

New Topic/Question
Reply




MultiQuote



|