2 Replies - 1266 Views - Last Post: 29 April 2009 - 07:31 AM Rate Topic: -----

#1 SassieAngel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 20-April 09

isinstance

Posted 23 April 2009 - 10:08 AM

ok i need your help once again....
I had the program working good except for 1 thing. If you entered any charcter other than a number it kicked you out of the program.

Well I told my teacher this and he inserted a IF isinstance statement.

and now it just loops on anything you enter including the numbers in the menu.

HELP!!!!!

def main():
	endProgram = 'no'
	integerchoice=0
	totalBudget=4000
	while endProgram == 'no':
		print
		print 'Welcome to the Personal Budget Program'
		print
		print 'Menu Selections'
		print '1-Add an Expense'
		print '2-Remove an Expense'
		print '3-Add Revenue'
		print '4-Remove Revenue'
		print '5-Check the Balance of your Budget'
		print '6-Exit'	 
		print
		choice=int(raw_input('Please enter your selection: '))
		if isinstance(choice,int):
			if choice == 1:
				totalBudget = addExpense(totalBudget)
			elif choice == 2:
				totalBudget = removeExpense(totalBudget)
			elif choice == 3:
				totalBudget = addRevenue(totalBudget)
			elif choice == 4:
				totalBudget = removeRevenue(totalBudget)
			elif choice == 5:
				print ('The balance you have left is: $%.2f ' )%totalBudget
			elif choice == 6:
				endProgram = 'yes'
				print 'Thank you for using our program, Have a Nice Day '
			elif choice != int:
				print 'I am sorry, That was an invalid selection, please try again.'
				print
		else:
			print "I am sorry, That was an invalid selection, please try again."


Is This A Good Question/Topic? 0
  • +

Replies To: isinstance

#2 atik97  Icon User is offline

  • ???
  • member icon

Reputation: 144
  • View blog
  • Posts: 715
  • Joined: 16-September 08

Re: isinstance

Posted 23 April 2009 - 10:34 AM

I have seen your previous code. If it's the only problem you kicked out of the program when you insert a character other than an integer, then this can be resolved by putting -
choice=raw_input('Please enter your selection: ')


instead of using int. Try this, it will work. Remember, in that case you must omit isinstance statement.
Just like this-
import sys
def main():
	integerchoice=0
	totalBudget=4000
	while True:
		choice=raw_input('enter your selection: ')
		if choice == '1':
				totalBudget = addExpense(totalBudget)
		elif choice == '2':
				totalBudget = removeExpense(totalBudget)
		elif choice == '3':
				totalBudget = addRevenue(totalBudget)
		elif choice == '4':
				totalBudget = removeRevenue(totalBudget)
		elif choice == '5':
			 print 'Your balance is',totalBudget
		elif choice == '6':
			 print 'Thank you for using my program, Goodbye'
			 sys.exit()
		elif choice != int:
			print 'Invalid selection, please try again'
	   
	   
	 
def addExpense(totalBudget):
	expense = input ('Enter expense amount: ')
	monthly = input ('How many time per month: ')
	totalExpense = expense * monthly
	totalBudget= totalBudget - totalExpense
	if totalExpense >= totalBudget:
		print ('The expenses was rejected because the budget exceeded.'),totalBudget
		totalBudget = expense * monthly + totalBudget	
	else:
		print ('The expenses was accepted, your remaining budget is :$'),totalBudget
	return totalBudget

def removeExpense (totalBudget):
	expense = input ('Enter expense amount: ')
	monthly = input ('How many time per month: ')
	totalExpense = expense * monthly
	totalBudget=totalExpense+totalBudget
	print('your remaining budget is %.2f' )%totalBudget
	return totalBudget

def addRevenue (totalBudget):
	revenue= input ('Enter new monthly income :')
	totalBudget=totalBudget+revenue
	print ('your new budget is :$%.2f') %totalBudget
	return totalBudget

def removeRevenue (totalBudget):
	revenue = input ('Enter new monthly income :')
	totalBudget = totalBudget - revenue
	if revenue >=totalBudget:
		print ('You owe :$%.2f')  %totalBudget
	else:
		print ('Your new budget is:$%.2f ') %totalBudget
	return totalBudget


print 'Welcome to the Personal Budget Program'
print 'Menu Selections'
print '1-Add an Expense'
print '2-Remove an Expense'
print '3-Add Revenue'
print '4-Remove Revenue'
print '5-Check Budget Balance'
print '6-Exit'  


main()


This post has been edited by shadhin: 23 April 2009 - 10:39 AM

Was This Post Helpful? 1
  • +
  • -

#3 SassieAngel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 20-April 09

Re: isinstance

Posted 29 April 2009 - 07:31 AM

thanks for that it worked,

but now it seems i need to validate input in the modules as well

any way you can point me in the right direction???



View Postshadhin, on 23 Apr, 2009 - 09:34 AM, said:

I have seen your previous code. If it's the only problem you kicked out of the program when you insert a character other than an integer, then this can be resolved by putting -
choice=raw_input('Please enter your selection: ')


instead of using int. Try this, it will work. Remember, in that case you must omit isinstance statement.
Just like this-
import sys
def main():
	integerchoice=0
	totalBudget=4000
	while True:
		choice=raw_input('enter your selection: ')
		if choice == '1':
				totalBudget = addExpense(totalBudget)
		elif choice == '2':
				totalBudget = removeExpense(totalBudget)
		elif choice == '3':
				totalBudget = addRevenue(totalBudget)
		elif choice == '4':
				totalBudget = removeRevenue(totalBudget)
		elif choice == '5':
			 print 'Your balance is',totalBudget
		elif choice == '6':
			 print 'Thank you for using my program, Goodbye'
			 sys.exit()
		elif choice != int:
			print 'Invalid selection, please try again'
	   
	   
	 
def addExpense(totalBudget):
	expense = input ('Enter expense amount: ')
	monthly = input ('How many time per month: ')
	totalExpense = expense * monthly
	totalBudget= totalBudget - totalExpense
	if totalExpense >= totalBudget:
		print ('The expenses was rejected because the budget exceeded.'),totalBudget
		totalBudget = expense * monthly + totalBudget	
	else:
		print ('The expenses was accepted, your remaining budget is :$'),totalBudget
	return totalBudget

def removeExpense (totalBudget):
	expense = input ('Enter expense amount: ')
	monthly = input ('How many time per month: ')
	totalExpense = expense * monthly
	totalBudget=totalExpense+totalBudget
	print('your remaining budget is %.2f' )%totalBudget
	return totalBudget

def addRevenue (totalBudget):
	revenue= input ('Enter new monthly income :')
	totalBudget=totalBudget+revenue
	print ('your new budget is :$%.2f') %totalBudget
	return totalBudget

def removeRevenue (totalBudget):
	revenue = input ('Enter new monthly income :')
	totalBudget = totalBudget - revenue
	if revenue >=totalBudget:
		print ('You owe :$%.2f')  %totalBudget
	else:
		print ('Your new budget is:$%.2f ') %totalBudget
	return totalBudget


print 'Welcome to the Personal Budget Program'
print 'Menu Selections'
print '1-Add an Expense'
print '2-Remove an Expense'
print '3-Add Revenue'
print '4-Remove Revenue'
print '5-Check Budget Balance'
print '6-Exit'  


main()


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1