首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >新atm程序问题

新atm程序问题
EN

Stack Overflow用户
提问于 2013-11-29 19:54:38
回答 2查看 1.7K关注 0票数 0

不管我做了什么,我都会不断地得到这个错误,我试着摆弄全局的名字等等,我仍然无法让这个错误消失:

代码语言:javascript
复制
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

我真的很想让它正常工作,这样我就能更好地理解代码是如何工作的。但是不管我看了多少个论坛,我还是想不出如何去修复它。

代码语言:javascript
复制
#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()
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-11-29 20:18:04

好的,您的代码中有许多问题将导致问题远远超出了错误讨论的范围。我会尽我所能,尽可能多地去经历它们。

首先,当您编写代码时,请尝试将您的函数与主代码分开,就像在顶部按正确的顺序定义它们,以便在定义它们之前不引用它们。

例如,在transaction()中,您调用定金(),但仍需声明订金()。它被定义在下面。

正如Martijn所说,您不能同时拥有一个存款函数和一个存款字符串。事情会开始变得非常混乱,而这正是导致错误的原因,而这个错误正在向你展示。您多次重复此错误,因此请确保更正所有实例。

接下来,确保当您调用一个函数时,您了解它需要哪些参数,并正确地传递它们。

您定义了def deposit(answer):,但是无论何时您调用定金,您都不会传递任何作为回答的内容。我甚至不知道为什么需要答案,因为您已经检查了它在事务中是'd‘还是’d‘。

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

我的最后一个注意事项(我不确定这是最后一个问题)是,您出于某种原因调用了主程序中的定金()、取款()和end(),并根据用户指定的内容在事务中调用它们。我认为从程序的外观来看,它们只应该在transaction()中调用。

如果你想不出什么办法的话,祝你好运和评论。

票数 0
EN

Stack Overflow用户

发布于 2013-11-29 19:57:00

您有一个deposit变量和一个deposit函数。你不能两者兼有,你需要重新命名其中一个或另一个。

Python函数是一流的对象。它们和值一样被绑定到名称中。它们不存在于与其他变量不同的名称空间中。

可能将函数重命名为make_deposit()

代码语言:javascript
复制
def make_deposit(answer):
    # ...

并称之为:

代码语言:javascript
复制
if answer[0].lower()=="d":
    make_deposit(answer)

您确实需要传递一个参数;您的函数需要一个参数(answer)。

这也适用于withdrawend

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20292569

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档