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

4
  • This is an indirect dupe of stackoverflow.com/questions/20449427/… ... Read that yo will understand Commented Jun 2, 2015 at 1:43
  • Is there a problem with my loop or my input? Commented Jun 2, 2015 at 1: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? Commented Jun 2, 2015 at 1:59
  • After all the edits i'm still stuck inside my while loop Commented Jun 2, 2015 at 2:33

4 Answers 4

4

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

withdrawAmount = float(raw_input(">"))
Sign up to request clarification or add additional context in comments.

1 Comment

I'm still stuck in the loop, it keeps asking me to enter new number.
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(...)

9 Comments

For some reason I'm still getting stuck inside my while loop, even after your edits
Try inserting print('Balance:', balance, 'withdrawAmount:', withdrawAmount) inside your loop, and take a look at what are values of those variables. Hope this helps.
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.
That's because you subtract withdrawAmount from balance before comparison. See edit.
Thanks for picking that error out. I guess I got a little too ahead of myself in the logic.
|
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.

Comments

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" 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.