【问题标题】:python while loop non-exitpython while循环不退出
【发布时间】:2015-08-15 15:36:27
【问题描述】:

我正在编写一个简单的程序,但我无法摆脱这个循环。我想要做的是,如果提款金额大于您的余额,请转到 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 循环中

【问题讨论】:

  • 这是stackoverflow.com/questions/20449427/…的间接欺骗...阅读你会明白的
  • 我的循环或输入有问题吗?
  • balance 的值是多少?您是在提供100 之类的输入,还是包含其他符号?你能举一个输入和balance的值的例子吗?错误是可重复的?
  • 在所有的编辑之后,我仍然卡在我的 while 循环中

标签: python python-2.7 loops while-loop


【解决方案1】:

raw_input() 返回一个字符串。您需要将其转换为 floatint,例如:

withdrawAmount = float(raw_input(">"))

【讨论】:

  • 我仍然卡在循环中,它一直要求我输入新号码。
【解决方案2】:

柯克是对的。

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 实际上是 intfloat,并在输入时将 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(...)

【讨论】:

  • 由于某种原因,即使在您编辑之后,我仍然卡在我的 while 循环中
  • 尝试在循环中插入print('Balance:', balance, 'withdrawAmount:', withdrawAmount),看看这些变量的值是什么。希望这会有所帮助。
  • 由于某种原因,Balance 打印出 -4000,我不知道该数字来自哪里,因为您可以看到余额从 0 开始,而在我的测试对象中,他的余额从 1000 开始。
  • 那是因为你在比较之前从balance 中减去了withdrawAmount。见编辑。
  • 感谢您找出该错误。我想我在逻辑上有点太超前了。
【解决方案3】:

试试这个:

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 循环后无论如何都会退出该块。

【讨论】:

    【解决方案4】:

    这是一种方法:

    balance = 100
    withdrawAmount = float(raw_input(">"))
    while withdrawAmount > balance:
        withdrawAmount = float(raw_input("Insufficient Funds, Enter new amount: "))
    print "Thank You" 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-15
      • 1970-01-01
      • 1970-01-01
      • 2015-07-14
      • 2016-08-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多