python while循环不退出
python while loop non-exit
我正在编写一个简单的程序,但我无法摆脱这个循环。我想要做的是,如果提款金额大于您的余额,请转到 while 循环。 while 循环应该得到一个新的输入并检查新的输入是否大于余额,如果是重复的,如果不是则转到 else,这是我打印余额的地方
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)
我的问题是无论我说什么我都卡在 while 循环中输入新金额
raw_input()
returns 一个字符串。您需要将其转换为 float
或 int
,例如:
withdrawAmount = float(raw_input(">"))
试试这个:
if withdrawAmount > balance:
while withdrawAmount > balance:
print "Insufficient Funds, Enter new amount"
withdrawAmount = int(raw_input())
这给了我这个(余额 = 50):
...
Try again
60
Try again
30
>>>
您不需要 else 语句,因为代码会在退出 while 循环后退出块。
柯克是对的。
raw_input()
生成字符串,而不是数值。我怀疑 balance
也是使用 raw_input()
创建的,是吗?如果是这样,那么您是在将字符串与字符串进行比较,而您认为您是在比较数字。这就是你陷入这个循环的原因。确保您具有预期类型的比较变量。
试试这个:
if float(withdrawAmount) > float(balance):
while float(withdrawAmount) > float(balance):
print("Insufficient Funds, Enter new amount")
withdrawAmount = raw_input(">")
else:
print
如果这可行,我的假设可能是正确的。
但我建议在这个片段之前检查您的代码以确保 balance
实际上是 int
或 float
,并将 withdrawAmount
设置为 float
(或int
)在输入处输入(正如柯克建议的那样);这样你就可以比较数字并且一切正常。
编辑:
好的,我发现你的代码有问题。实际上,在 比较它们之前,您从余额 中减去 withdrawAmount。试试这个:
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(...)
这是一种方法:
balance = 100
withdrawAmount = float(raw_input(">"))
while withdrawAmount > balance:
withdrawAmount = float(raw_input("Insufficient Funds, Enter new amount: "))
print "Thank You"
我正在编写一个简单的程序,但我无法摆脱这个循环。我想要做的是,如果提款金额大于您的余额,请转到 while 循环。 while 循环应该得到一个新的输入并检查新的输入是否大于余额,如果是重复的,如果不是则转到 else,这是我打印余额的地方
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)
我的问题是无论我说什么我都卡在 while 循环中输入新金额
raw_input()
returns 一个字符串。您需要将其转换为 float
或 int
,例如:
withdrawAmount = float(raw_input(">"))
试试这个:
if withdrawAmount > balance:
while withdrawAmount > balance:
print "Insufficient Funds, Enter new amount"
withdrawAmount = int(raw_input())
这给了我这个(余额 = 50):
...
Try again
60
Try again
30
>>>
您不需要 else 语句,因为代码会在退出 while 循环后退出块。
柯克是对的。
raw_input()
生成字符串,而不是数值。我怀疑 balance
也是使用 raw_input()
创建的,是吗?如果是这样,那么您是在将字符串与字符串进行比较,而您认为您是在比较数字。这就是你陷入这个循环的原因。确保您具有预期类型的比较变量。
试试这个:
if float(withdrawAmount) > float(balance):
while float(withdrawAmount) > float(balance):
print("Insufficient Funds, Enter new amount")
withdrawAmount = raw_input(">")
else:
print
如果这可行,我的假设可能是正确的。
但我建议在这个片段之前检查您的代码以确保 balance
实际上是 int
或 float
,并将 withdrawAmount
设置为 float
(或int
)在输入处输入(正如柯克建议的那样);这样你就可以比较数字并且一切正常。
编辑:
好的,我发现你的代码有问题。实际上,在 比较它们之前,您从余额 中减去 withdrawAmount。试试这个:
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(...)
这是一种方法:
balance = 100
withdrawAmount = float(raw_input(">"))
while withdrawAmount > balance:
withdrawAmount = float(raw_input("Insufficient Funds, Enter new amount: "))
print "Thank You"