Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ATM程序Python_Python_Python 3.x - Fatal编程技术网

ATM程序Python

ATM程序Python,python,python-3.x,Python,Python 3.x,我用python为一个简单的ATM程序提交了这段代码。我得到了满分,但被告知不要使用全局变量,因为它们被认为是糟糕的编程。我已经尝试重写这个函数一百万次了,但是当我重写的时候,我的余额并没有更新。有人能告诉我我做错了什么吗?我自己也弄不明白,这让我很烦恼 我的原件: name=input("Name on your bank account? ") balance=float(input("Your current balance? ")) def printMenu(): print

我用python为一个简单的ATM程序提交了这段代码。我得到了满分,但被告知不要使用全局变量,因为它们被认为是糟糕的编程。我已经尝试重写这个函数一百万次了,但是当我重写的时候,我的余额并没有更新。有人能告诉我我做错了什么吗?我自己也弄不明白,这让我很烦恼

我的原件:

name=input("Name on your bank account? ")
balance=float(input("Your current balance? "))

def printMenu():
    print(name,"Welcome to the Lots of Money Bank")
    print("Enter 'b'(balance), 'd'(deposit), 'w'(withdraw), or'q'(quit)")

def getTransaction():
    transaction=str(input("What would you like to do? "))
    return transaction

def withdraw(bal,amt):
    global balance
    balance=bal-amt
    if balance<0:
        balance=balance-10

def formatCurrency(amt):
    return "$%.2f" %amt

###MAIN PROGRAM###

printMenu()
command=str(getTransaction())

while command!="q":
    if (command=="b"):
        print(name,"Your current balance is",formatCurrency(balance))
        printMenu()
        command=str(getTransaction())
    elif (command=="d"):
        amount=float(input("Amount to deposit? "))
        balance=balance+amount
        printMenu()
        command=str(getTransaction())
    elif (command=="w"):
        amount=float(input("Amount to withdraw? "))
        withdraw(balance,amount)
        printMenu()
        command=str(getTransaction())
    else:
        print("Incorrect command. Please try again.")
        printMenu()
        command=str(getTransaction())

print(name,"Goodbye! See you again soon")
name=input(“您银行账户上的姓名?”)
余额=浮动(输入(“您当前的余额?”)
def printMenu():
打印(名称:“欢迎来到大量资金银行”)
打印(“输入‘b’(余额)、‘d’(存款)、‘w’(取款)或‘q’(退出)”)
def getTransaction():
transaction=str(输入(“您想做什么?”)
退货交易
def提取(余额、金额):
全球平衡
余额=余额金额
如果余额您从未使用从
取款
返回的金额更新“全局”
余额

试试这个:

# and my edited elif
elif (command=="w"):
        amount=float(input("Amount to withdraw? "))
        # update balance!
        balance = withdraw(balance,amount)
        printMenu()
        command=str(getTransaction())
而且

def提取(余额、金额):
#“平衡”与全球平衡息息相关——我们不想用它
#这就是为什么我们有“bal”(通过参数传递)
#bal=balance#无需,调用函数时bal=balance
#金额=金额#与余额相同
如果在
draw()
函数中(bal),则不应引用全局变量
balance
amount
。调用函数时只需更新
balance
,因此我们有:

def withdraw(bal,amt):
    if(bal<0):
        bal=bal-10
    else:
        bal=bal-amt
    return bal

非常感谢!我知道这是非常明显的事情,我就是看不见!这是一个很好的答案。我还要补充一点,永远不要将未分组到类中的变量作为属性。对于您的示例,我会这样做:class G():name=input(“银行帐户上的名称?”)balance=float(输入(“您当前的余额?”)并通过G.balance和G.name访问您可以想象如何将其轻松扩展为更详细的银行账户对象类(如果函数需要多个属性,您可以传递整个实例,而不是逐个变量)。在elif中,您需要bal=draw(余额,金额)--现在您只是忽略了draw的返回值!(代码中还有很多其他气味,但这就是您要问的具体错误)。
def withdraw(bal,amt):
    # 'balance' is bound to global balance - we don't want to use that
    # this is why we have 'bal' (passed through parameters)
    # bal = balance # no need, bal=balance when function is called
    # amt=amount    # same as balance
    if(bal<0):
        bal=bal-10
    else:
        bal=bal-amt
    return bal
def withdraw(bal,amt):
    if(bal<0):
        bal=bal-10
    else:
        bal=bal-amt
    return bal
elif (command=="w"):
        amount=float(input("Amount to withdraw? "))
        balance = withdraw(balance,amount)
        printMenu()
        command=str(getTransaction())