3 Replies - 6269 Views - Last Post: 30 April 2009 - 09:03 AM Rate Topic: -----

#1 SassieAngel  Icon User is offline

  • New D.I.C Head

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

validating input in python

Posted 29 April 2009 - 09:20 AM

once again i could use a little advice

it seems i need to validate the input into addExpense module.
well i can get it to recognize the numbers and the return key
HOWEVER
when i enter a letter... a b c etc,
it kicks me out of the program

any advice would be a help

im programming in python 2.5 at school

thanks in advance


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=raw_input('Please 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 ('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 is an invalid selection, Please try again!  '

def validValue(amount):
	if isinstance(amount, int) or isinstance(amount, str):  # check the value is integer
		
		if amount =='' :
			amount = 0
			return amount
		else:
			amount = int(amount) # convert value
			return amount # return value
	 
	else:
		 
		return 0  # if value is not integer then make it zero
	
def addExpense(totalBudget):
	expense = raw_input ('Enter expense amount: $')
	expense = validValue(expense)
	
	monthly= raw_input ('How many time per month: ')
	monthly = validValue(monthly)
	
	totalExpense = expense * monthly
	if totalExpense > totalBudget:
		newBudget = totalBudget - totalExpense
		newBudget = newBudget * -1	#Turns the negative value into a positive just for display purposes
		
		print ('The expenses was rejected because the budget exceeded by: $ %.2f')% newBudget

		newBudget = newBudget * -1	#Turns the positive value back to a negative for correct calculations
		newBudget2 = expense * monthly + newBudget

		totalBudget = newBudget2
		newBudget = 0	#Resets temporary calculation variables back to 0 for future calculations
		newBudget2 = 0	#Resets temporary calculation variables back to 0 for future calculations

	else:
		newBudget = totalBudget - totalExpense
		
		print ('The expenses was accepted, your remaining budget is : $ %.2f')%newBudget

		totalBudget = newBudget
		newBudget = 0	#Resets temporary calculation variables back to 0 for future calculations
	return totalBudget


def removeExpense (totalBudget):
	expense = input ('Enter expense amount: $')
	monthly = input ('How many time per month: ')

	removal = expense * monthly
	newBudget = removal + totalBudget

	print('your remaining budget is: $ %.2f')%newBudget

	totalBudget = newBudget
	removal = 0	#Resets temporary calculation variables back to 0 for future calculations
	newBudget = 0	#Resets temporary calculation variables back to 0 for future calculations

	return totalBudget

def addRevenue (totalBudget):
	revenue = input ('Enter new monthly income : $ ')
	newBudget = totalBudget + revenue

	print ('your new budget is :$ %.2f') %newBudget

	totalBudget = newBudget
	newBudget = 0	#Resets temporary calculation variables back to 0 for future calculations

	return totalBudget

def removeRevenue (totalBudget):
	remRevenue = input ('Enter amount of monthly income to be removed : $')
	if remRevenue <= totalBudget:
		newBudget = totalBudget - remRevenue

		print ('Your new balance is:$ %.2f') %newBudget

		totalBudget = newBudget
		newBudget = 0	#Resets temporary calculation variables back to 0 for future calculations

	else:
		newBudget = totalBudget - remRevenue
		newBudget = newBudget * -1	#Turns the negative value into a positive just for display purposes

		print ('You went over the budget by:$ %.2f ') %newBudget

		newBudget = newBudget * -1	#Turns the positive value back to a negative for correct calculations

		totalBudget = newBudget
		newBudget = 0	#Resets temporary calculation variables back to 0 for future calculations

	return totalBudget

main()



This post has been edited by SassieAngel: 29 April 2009 - 09:21 AM


Is This A Good Question/Topic? 0
  • +

Replies To: validating input in python

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: validating input in python

Posted 29 April 2009 - 09:31 AM

You can use the isdigit() function:

def validValue(amount):
	if amount.isdigit():
		#do integer stuff
	else:
		amount = 0  #assign zero if its not a digit
	return amount


Was This Post Helpful? 0
  • +
  • -

#3 SassieAngel  Icon User is offline

  • New D.I.C Head

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

Re: validating input in python

Posted 29 April 2009 - 12:47 PM

ok im with you so far.... but if the data is put in as decimal....

ex(9.50) it doesnt recognize it
it does not calculate right





View PostKYA, on 29 Apr, 2009 - 08:31 AM, said:

You can use the isdigit() function:

def validValue(amount):
	if amount.isdigit():
		#do integer stuff
	else:
		amount = 0  #assign zero if its not a digit
	return amount


Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: validating input in python

Posted 30 April 2009 - 09:03 AM

You could do a type check:

if type(number) == type(1) #or whatever the number is, there's a chart somewhere


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1