2

I'm writing a simple program and I just can't get out of this loop. What i want to do is if withdraw amount is greater than your balance, go to the while loop. The while loop should get a new input and check if new input is greater than balance, if it is repeat, if not go to the else, which is where i print the balance

class Account(object):
balance = 0
accountNumber = 0

def __init__(self, f, l, ssn, b):
    self.firstName = f
    self.lastName = l
    self.socialSecurity = ssn
    self.balance = b
    self.accountNumber = randint(0, 10000)

def __str__(self):
    return self.firstName + " " + self.lastName + \
           "'s balance is $" + str(self.balance) + \
           ". Your account Number is " + str(self.accountNumber)

def deposit(self, amount):
    depositAmount = amount
    balance = self.balance + depositAmount

    print(str(depositAmount) + " has been deposited into account "
                               "#" + str(
        self.accountNumber) + " Your balance is "
                              "now " + str(balance))
    return self.balance

def withdraw(self, amount):
    withdrawAmount = amount
    balance = self.balance - withdrawAmount
    if float(withdrawAmount) > float(balance):
        while float(withdrawAmount) > float(balance):
            print("Insufficient Funds, Enter new amount")
            withdrawAmount = raw_input(">")
    else:
       print(str(withdrawAmount) + " has been taken out of account "
                                "#" + str(
        self.accountNumber) + " Your balance is "
                              "now " + str(balance))



testOne = Account("John", "Smith", "1111", 1000)
print(testOne)
print(testOne.accountNumber)
testOne.deposit(200)
testOne.withdraw(5000)

my problem is that i'm stuck in the while loop no matter what i put it says enter new amount

jumpman8947
  • 427
  • 2
  • 7
  • 17
  • This is an indirect dupe of http://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-integers-in-python ... Read that yo will understand – Bhargav Rao Jun 02 '15 at 01:43
  • Is there a problem with my loop or my input? – jumpman8947 Jun 02 '15 at 01:53
  • what is the value of `balance`? Are you giving inputs like `100` or are you including other symbols? Can you give an example of an input and a value of `balance` with which the error is repeatable? – mattsilver Jun 02 '15 at 01:59
  • After all the edits i'm still stuck inside my while loop – jumpman8947 Jun 02 '15 at 02:33

4 Answers4

4

raw_input() returns a string. You need to cast that to a float or int, like:

withdrawAmount = float(raw_input(">"))
Kirk Strauser
  • 30,189
  • 5
  • 49
  • 65
3

Kirk is right.

raw_input() produces strings, not numeric values. I suspect that balance was also created using raw_input(), is that so? If so, you are comparing string to a string, while you think you compare numbers. That is why you are stuck in this loop. Make sure you have the intended types of compared variables.

Try this:

if float(withdrawAmount) > float(balance):
        while float(withdrawAmount) > float(balance):
            print("Insufficient Funds, Enter new amount")
            withdrawAmount = raw_input(">")
else:
    print

If this works, I am probably right in my assumptions.

But I would advise to review your code before this fragment to make sure balance is actually an int or float, and also set withdrawAmount to float (or int) type at input (as Kirk suggests); this way you will be comparing numbers and all will work fine.

EDIT:

Ok I see a problem in your code. You actually subtract withdrawAmount from balance before you compare them. Try this:

def withdraw(self, amount):
     withdrawAmount = amount
     balance = self.balance
     while withdrawAmount > balance:
         print("Insufficient Funds, Enter new amount")
         withdrawAmount = int(raw_input(">"))
     balance = balance - withdrawAmount
     print(...)
M N
  • 46
  • 4
  • For some reason I'm still getting stuck inside my while loop, even after your edits – jumpman8947 Jun 02 '15 at 02:33
  • Try inserting `print('Balance:', balance, 'withdrawAmount:', withdrawAmount`) inside your loop, and take a look at what are values of those variables. Hope this helps. – M N Jun 02 '15 at 02:35
  • 1
    For some reason Balance is printing out -4000, i don't know where that number is coming from because you can see balance started at 0, and in my test object his balance starts at 1000. – jumpman8947 Jun 02 '15 at 02:42
  • 1
    That's because you subtract `withdrawAmount` from `balance` before comparison. See edit. – M N Jun 02 '15 at 02:49
  • 1
    Thanks for picking that error out. I guess I got a little too ahead of myself in the logic. – jumpman8947 Jun 02 '15 at 02:54
  • It works initially but, there still seems to be an error here, in testOne object i try to withdraw 5000 which draws the loop. then if i put 10 it goes through fine, but if i put 2000, then 10000, then 5, it would say insufficient funds to that 5. – jumpman8947 Jun 02 '15 at 02:57
  • I suggest to monitor variables using `print` again, it will help you see how they change with each input. – M N Jun 02 '15 at 03:01
  • I fixed it, that my mistake, i had the balance = balance - withdraw in the while loop which caused it to change upon every input. – jumpman8947 Jun 02 '15 at 03:03
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/79378/discussion-between-m-n-and-jumpman8947). – M N Jun 02 '15 at 03:05
0

Try this:

if withdrawAmount > balance:
    while withdrawAmount > balance:
        print "Insufficient Funds, Enter new amount"
        withdrawAmount = int(raw_input())

Which gives me this (balance = 50):

...
Try again
60
Try again
30
>>>

You don't need the else statement because the code will exit the block anyway after exiting the while loop.

Sean Saito
  • 655
  • 7
  • 18
0

This is one way to do it:

balance = 100
withdrawAmount = float(raw_input(">"))
while withdrawAmount > balance:
    withdrawAmount = float(raw_input("Insufficient Funds, Enter new amount: "))
print "Thank You" 
Joe T. Boka
  • 6,554
  • 6
  • 29
  • 48