Budget planner using python

I need help with the code for keepig it from crashing if the wrong inp

Page 1 of 1

1 Replies - 3137 Views - Last Post: 29 August 2009 - 07:22 AM Rate Topic: -----

#1 Dcaldwell  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 28-August 09

Budget planner using python

Posted 29 August 2009 - 01:13 AM

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 != "":
	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'

def addExp(income):
   amt = input('**Enter expense to add: ')

   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):
   amt = input('**Enter expense amount to be removed: ')
   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):
	amt = input('**Enter income to add: ')

	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):
	amt = input('**Enter income amount to be removed: ')
	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):

   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)


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.

Is This A Good Question/Topic? 0
  • +

Replies To: Budget planner using python

#2 Nallo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 165
  • View blog
  • Posts: 258
  • Joined: 19-July 09

Re: Budget planner using python

Posted 29 August 2009 - 07:22 AM

You may want to tell us, how you crashed your programm and what error message you got then. It is a little difficult to help you without that information.

Anyway, one way to handle user input with the try except satement would be as follows:
while True:
	user_input = raw_input("enter some value")
		data = int(user_input) #or float(...) for floats
	except ValueError:
		print "invalid input, try again"
# data is an integer when the loop is left

Explanation: if int(user_input) fails, a ValueError Exception is thrown, and caught in the except block. If no exception is caught the else block is executed and break command leves the while loop, data is an integer.
If an exception was caught, the else block is not executed and the loop continues.

One more important comment on your code:
Dont use input(). Use raw_input() instead.

Input is not safe. For example look at the following code:
import os
user_input = input("crash me please")

What happens, if a user inputs os.system("rm file")?

With input() Python tries to evaluate whatever is entered as a python expression ... So Python calls the os.system function with the argument "rm file", which in turn executes the shell command "rm file" and removes file from your disk. For a real headache, assume it was "cd /;rm -r *" on Linux and your programm run with root rights.

This post has been edited by Nallo: 29 August 2009 - 06:20 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1