By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,737 Members | 1,310 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,737 IT Pros & Developers. It's quick & easy.

Python: Negative result when subtracting two positive values

Bazooka9T
P: 3
Hey group,
I am new on here, and also to Python. I have been setting myself various tasks to try to learn more about this language and have a small query.
I set myself a "Cash Machine" task, and although I am still yet to add multiples of 10, overdraft etc.... I keep getting a negative value as a result which is literally "bugging" me *sticks fork in leg for the sad pun.
Any ideas on where exactly right under my nose I have booboo'd? Even with a positive result, the script outputs a negative value.

Thanks.
Expand|Select|Wrap|Line Numbers
  1. ######################################
  2. #!/usr/bin/python -tt
  3. # -*- coding: cp1252 -*-
  4.  
  5. from random import choice
  6. import sys, math
  7.  
  8. class BreakLoop:
  9.     pin_attempts = "PIN failed to authorise on three attempts"
  10.     card_returned = "Card returned to account holder"
  11.  
  12. class Account_Detail(object):
  13.     def __init__(self, name, surname, balance, account, PIN):
  14.         self.name = name
  15.         self.surname = surname
  16.         self.balance = balance
  17.         self.account = account
  18.         self.PIN = PIN
  19.  
  20. ### Populate database
  21. acc1 = Account_Detail('Jeff', 'Minter', 200.18, 789321, 1234)
  22. acc2 = Account_Detail('Tim', 'Lambesis', 182.63, 147852, 7896)
  23. acc3 = Account_Detail('Matt', 'Heafy', 350.58, 963258, 9874)
  24.  
  25. ### Prep and setup random choice for entering card into machine
  26. account1 = [acc1.name, acc1.surname, acc1.balance, acc1.account, acc1.PIN]
  27. account2 = [acc2.name, acc2.surname, acc2.balance, acc2.account, acc2.PIN]
  28. account3 = [acc3.name, acc3.surname, acc3.balance, acc3.account, acc3.PIN]
  29.  
  30. rand_acc = [account1, account2, account3]
  31. card_choice = choice(rand_acc)
  32.  
  33. print card_choice[4]
  34.  
  35. def ins_card():
  36.     count = 0
  37.     while count < 3:
  38.         print "Your account number is %i" % (card_choice[3])
  39.         card_in = input ("Please enter your PIN: ")
  40.         if card_in == (card_choice[4]):
  41.             print "Hello, %s %s" %(card_choice[0], card_choice[1])
  42.             print "Your account balance is %.2f " % (card_choice[2])
  43.             count = 4
  44.         elif card_in == (''):
  45.             print "No PIN entered"
  46.         else:
  47.             print "Incorrect PIN"
  48.             count = count + 1
  49.  
  50.         if count == 3:
  51.             print; print "PIN entry has exceeded three attempts" 
  52.             sys.exit(BreakLoop.pin_attempts)  
  53.  
  54. def withdraw():        
  55.     amount = input ("How much would you like to withdraw?: ")
  56.     if amount <= card_choice[2]:
  57.         #print amount
  58.         #print card_choice[2]
  59.         tot_bal = amount - card_choice[2]
  60.         print; print "Dispensing %i" % (amount)
  61.         print "Your new balance is %.2f" % (tot_bal)
  62.         print "Have a nice day"
  63.     else:
  64.         funds = "You do not have sufficient funds available"
  65.         print; print funds.upper() 
  66.         sys.exit(BreakLoop.card_returned)
  67.  
  68. if __name__ == "__main__":
  69.     ins_card()
  70.     withdraw()
  71. ####################################
Feb 27 '13 #1
Share this Question
Share on Google+
6 Replies

bvdet
Expert Mod 2.5K+
P: 2,851
Change this line:
Expand|Select|Wrap|Line Numbers
  1. tot_bal = amount - card_choice[2]
to this:
Expand|Select|Wrap|Line Numbers
  1. tot_bal = card_choice[2] - amount
Feb 27 '13 #2

Expert 100+
P: 626
i.e. your logic is bass-ackwards
Expand|Select|Wrap|Line Numbers
  1.     if amount <= card_choice[2]:
  2.          #print amount
  3.          #print card_choice[2]
  4.          tot_bal = amount - card_choice[2] 
Feb 27 '13 #3

Bazooka9T
P: 3
My mind told me to do that, but I'm fiddling at work and thought "nah! That's not it"
A serious lesson learnt here, and a pathetically stupid schoolboy error.

Thank you, I feel like a fool (because I am)
Feb 27 '13 #4

bvdet
Expert Mod 2.5K+
P: 2,851
You shouldn't feel that way. Sometimes the simplest errors are the hardest to find. The best way to learn is to make mistakes.
Feb 27 '13 #5

Expert 100+
P: 626
I prefer to avoid the top if statement as it can be confusing sometimes and use instead
Expand|Select|Wrap|Line Numbers
  1. tot_bal = amount - card_choice[2] 
  2. if tot_bal >= 0: 
Feb 27 '13 #6

Bazooka9T
P: 3
@bvdet I'm cool, I just kick myself when I miss the obvious. But as you say, these errors make us stronger.
@dwblas I will look at that, good tip!

I'm still expanding with this, I have found it a good project to test the things I have thus far learnt.
Thank you both for your input, I hope one day I will be assisting other newbies with such issues.
Feb 27 '13 #7

Post your reply

Sign in to post your reply or Sign up for a free account.