简体   繁体   中英

python while loop non-exit

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

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

withdrawAmount = float(raw_input(">"))

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(...)

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.

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" 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM