【问题标题】:Withdraw and Deposit with Recursion递归取款和存款
【发布时间】:2015-05-10 01:12:14
【问题描述】:

所以我刚刚开始研究这个以练习我的递归经验。我在这里有我的代码,出于某种原因,它只允许我使用其中的提款部分,而不是存款:

def recursive(n):
    print("You have $", n, "In the bank!")
    option = (input("Do you want to withdraw or deposit?"))    
    if option == "withdraw" or "Withdraw":
        withdraw = int(input("How much do you want to withdraw from your account?"))
        recursive(n - withdraw)

    elif option == "deposit" or "Deposit":
        deposit = int(input("How much do you want to deposit?"))
        recursive(n + deposit)

    else:
        print("Not a valid option!")
        print("Shutting Down!")

def money(n):
    if n < 0:
        print("You are out of money!")

def main():
    recursive(100)

main()

请在这里告诉我我的错误!

【问题讨论】:

  • @QPaysTaxes 即使我输入了存款,它也只会让我做提款部分
  • 未来,您需要将所有相关信息放入问题本身。放在 cmets 中还不错,但是放在一个地方会更整洁。

标签: python recursion


【解决方案1】:

你应该改变:

if option == "withdraw" or "Withdraw":

到:

if option == "withdraw" or option == "Withdraw":

deposit 也一样。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-26
    • 2019-11-04
    • 2020-11-20
    • 1970-01-01
    相关资源
    最近更新 更多