新的atm计划问题

时间:2013-11-29 19:54:38

标签: python python-3.x

无论我做什么,我一直都会收到这个错误,我试图摆弄全球名称之类的东西,我仍然无法让这个错误消失:

Traceback (most recent call last):
  File "/Users/tonystrobach/Desktop/untitled4.py", line 48, in <module>
    transaction()
  File "/Users/tonystrobach/Desktop/untitled4.py", line 37, in transaction
    deposit()
TypeError: 'str' object is not callable

我真的想让它正常运行,以便我能更好地理解代码的工作原理。但无论我看多少个论坛,我仍然无法弄清楚如何修复它。

#The purpose of this program is to create a simple ATM environment
#Written by K. Strobach 11/22/13

withdraw = str(0)
deposit = str (0)
end = str(0)

def account():
    print("Hello, welcome to Bank of Python!\nPlease enter your account pin number")
    account = input("Account pin number is 1234: ")
    account = 1234

account()

def accbalance():
    print ("would you like to see your account balance?")
    see = input("Enter Y or N: ")

    if (see == "Y") or (see == "y"):
        balance = str(0)
        print("Current balance: $", str(balance))

accbalance()

def transaction():
    global withdraw
    global deposit
    global end
    print ("Would you like to make a transaction?")
    more = input("Enter Y or N: ")

    if (more == "Y") or (more == "y"):
        print ("Would you like to make a deposit or withdraw? Or would you like to end this transaction?")
        answer = input("Enter D for deposit; W for withdraw; E for end: ")

    if answer[0].lower()=="d":
        deposit()

    if answer[0].lower()=="w":
        withdraw()

    if answer[0].lower()=="e":
        end()

    else:
        print("Thank you for banking with Bank of Python, goodbye!")

transaction()

def deposit(answer):
        if (answer == "D") or (answer == "d"):
            print ("How much would you like to deposit?")
            deposit = input("Enter amount to deposit: ")
            deposit = str(deposit)

            currentbalance = deposit + balance
            print ("Deposited: $", + str(deposit))
            print ("Current Balance: $", + str(currentbalance))

deposit()

def withdraw(answer):    
        if (answer == "W") or (answer == "w"):
            print ("How much would you like to withdrawl?")
            withdraw = input("Enter amount to withdraw: ")
            withdraw = str(withdraw)

            currentbalance = withdraw - balance
            print ("Withdrew: $", + str(withdraw))
            print ("Current Balance: $", + str(currentbalance))

withdraw()

def end(answer):
    if (answer == "E") or (answer == "e"):
        print("Thank you for banking with Bank of Python, goodbye!")
end()

2 个答案:

答案 0 :(得分:1)

您同时拥有deposit变量 deposit功能。你不能同时拥有这两者,你需要重命名其中一个。

Python函数是第一类对象。它们与名称绑定在一起。它们与其他变量不在一个单独的命名空间中。

将函数重命名为make_deposit()或许:

def make_deposit(answer):
    # ...

并称之为:

if answer[0].lower()=="d":
    make_deposit(answer)

你需要传递一个参数;你的函数需要一个(answer)。

这同样适用于withdrawend

答案 1 :(得分:0)

好的,您的代码存在许多问题,这些问题将导致问题远远超出该错误的讨论范围。我将尝试尽可能多地完成它们。

首先,当您编写代码时,请尝试保留您的功能,与主代码分开,如在顶部和正确的顺序中定义它们,以便在定义之前不引用它们。
例如在transaction()中你调用deposit()但是还没有声明deposit()。它定义如下。

正如Martijn所说,你不能同时拥有存款功能和存款字符串。事情将开始变得非常混乱,这实际上是导致显示给你的错误的原因。你多次重复这个错误,所以一定要纠正所有的情况。

接下来确保在调用函数时,您了解它需要哪些参数并正确传递它们 例如你定义def deposit(answer):但是当你打电话给押金时,你不会把它作为答案通过。我甚至不确定为什么需要回答,因为你已经检查过它是否是一个&#39; d&#39;或&#34; d&#34;在交易中。

我的下一个问题是,您总是在不需要时定义全局变量。如果变量仅在一个函数中使用,就像你的所有函数一样,那么它们只需要在该函数中定义。由于python具有隐式类型而不是在全局范围内声明存储字符串,因此将在程序运行第deposit = input("Enter amount to deposit: ")行时创建它。另外,您应该注意,当您尝试将存款更改为int时,您应该在编写int(deposit)时编写str(deposit),并将其转换为字符串。

我的最后一点(我不确定这是最后一个问题)是你出于某种原因在主程序中调用deposit(),withdraw()和end()以及调用它们在事务中基于用户指定的内容。我想从程序看起来它们应该只在transaction()中调用。

祝你好运和评论,如果你无法解决问题。