Python: Negative result when subtracting two positive values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bazooka9T
    New Member
    • Feb 2013
    • 3

    Python: Negative result when subtracting two positive values

    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.
    Code:
    ######################################
    #!/usr/bin/python -tt
    # -*- coding: cp1252 -*-
    
    from random import choice
    import sys, math
    
    class BreakLoop:
        pin_attempts = "PIN failed to authorise on three attempts"
        card_returned = "Card returned to account holder"
    
    class Account_Detail(object):
        def __init__(self, name, surname, balance, account, PIN):
            self.name = name
            self.surname = surname
            self.balance = balance
            self.account = account
            self.PIN = PIN
    
    ### Populate database
    acc1 = Account_Detail('Jeff', 'Minter', 200.18, 789321, 1234)
    acc2 = Account_Detail('Tim', 'Lambesis', 182.63, 147852, 7896)
    acc3 = Account_Detail('Matt', 'Heafy', 350.58, 963258, 9874)
    
    ### Prep and setup random choice for entering card into machine
    account1 = [acc1.name, acc1.surname, acc1.balance, acc1.account, acc1.PIN]
    account2 = [acc2.name, acc2.surname, acc2.balance, acc2.account, acc2.PIN]
    account3 = [acc3.name, acc3.surname, acc3.balance, acc3.account, acc3.PIN]
    
    rand_acc = [account1, account2, account3]
    card_choice = choice(rand_acc)
    
    print card_choice[4]
    
    def ins_card():
        count = 0
        while count < 3:
            print "Your account number is %i" % (card_choice[3])
            card_in = input ("Please enter your PIN: ")
            if card_in == (card_choice[4]):
                print "Hello, %s %s" %(card_choice[0], card_choice[1])
                print "Your account balance is £%.2f " % (card_choice[2])
                count = 4
            elif card_in == (''):
                print "No PIN entered"
            else:
                print "Incorrect PIN"
                count = count + 1
                
            if count == 3:
                print; print "PIN entry has exceeded three attempts" 
                sys.exit(BreakLoop.pin_attempts)  
           
    def withdraw():        
        amount = input ("How much would you like to withdraw?: ")
        if amount <= card_choice[2]:
            #print amount
            #print card_choice[2]
            tot_bal = amount - card_choice[2]
            print; print "Dispensing £%i" % (amount)
            print "Your new balance is £%.2f" % (tot_bal)
            print "Have a nice day"
        else:
            funds = "You do not have sufficient funds available"
            print; print funds.upper() 
            sys.exit(BreakLoop.card_returned)
                    
    if __name__ == "__main__":
        ins_card()
        withdraw()
    ####################################
    Last edited by bvdet; Feb 27 '13, 02:58 PM. Reason: Please use code tags when posting code [code]....[/code]
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Change this line:
    Code:
    tot_bal = amount - card_choice[2]
    to this:
    Code:
    tot_bal = card_choice[2] - amount

    Comment

    • dwblas
      Recognized Expert Contributor
      • May 2008
      • 626

      #3
      i.e. your logic is bass-ackwards
      Code:
          if amount <= card_choice[2]:
               #print amount
               #print card_choice[2]
               tot_bal = amount - card_choice[2]

      Comment

      • Bazooka9T
        New Member
        • Feb 2013
        • 3

        #4
        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)

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          You shouldn't feel that way. Sometimes the simplest errors are the hardest to find. The best way to learn is to make mistakes.

          Comment

          • dwblas
            Recognized Expert Contributor
            • May 2008
            • 626

            #6
            I prefer to avoid the top if statement as it can be confusing sometimes and use instead
            Code:
            tot_bal = amount - card_choice[2] 
            if tot_bal >= 0:

            Comment

            • Bazooka9T
              New Member
              • Feb 2013
              • 3

              #7
              @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.

              Comment

              Working...