【问题标题】:Python exercise using a dictionary to implement withdrawals and deposits to bank accountsPython练习使用字典实现银行账户的取款和存款
【发布时间】:2015-03-14 14:49:21
【问题描述】:

我是 Python 编程的初学者,因此我正在学习每周必须提交作业的课程。

目前的任务是编写一个程序,用户可以打开,关闭和账户,可以存款和取款,以及列出账户(即根据特定账户查看所有账户和余额)。

我的主要问题是每个帐户的余额(从 0 开始)都是相同的,而每个帐户都应该有自己的余额!

我走了这么远:

# -*- coding: utf-8 -*-
def print_menu():
    print "1. List Account(s)"
    print "2. Open an Account"
    print "3. Close an Account"
    print "4. Withdraw money"
    print "5. Deposit Money"
    print "6. Quit"
    print

accounts = {}
menu_choice = 0
print_menu()
#balance = {}
balance = 0
while True:
    menu_choice = int(input("Type in a number (1-6): "))
    print
    if menu_choice == 1:
        print "Accounts"
        for x in accounts.keys():
            print "Accountnumber:", x, "with balance €", balance
        print
    elif menu_choice == 2:
        print "Enter a new Accountnumber"
        number = input("New accountnumber: ")
        #phone = input("Number: ")
        accounts[number]=balance
        print "Accountnumber", number, "opened."
    elif menu_choice == 3:
        print "Close an Accountnumber"
        number = input("Accountnumber: ")
        if number in accounts:
            #accounts[number]=balance
            del accounts[number]
            print "Accountnumber", number, "is closed."
        else:
            print "Accountnumber", number, "was not found"
    #elif menu_choice == 4:
        #print("Lookup Number")
        #name = input("Name: ")
        #if name in numbers:
        #    print("The number is", numbers[name])
        #else:
        #    print(name, "was not found")
    elif menu_choice == 4:
        print "Withdraw money from Account."
        number = input("Accoutnnumber: ")
        if number in accounts:
            withdraw = float(input("How much money would you like to withdraw? > "))
            if withdraw < balance:
                #accounts[number]=balance
                numbers[balance] -= withdraw
                #balance vann number 444 bijv. !!
                print "Your new balance is €", balance
            else:
                print "There are no sufficient funds on this accountnumber"
    elif menu_choice == 5:
        print "Deposit money onto Account."
        number = input("Accountnumber: ")
        if number in accounts:
            deposit = float(input("How much money would you like to deposit? > "))
            #accounts[number]=balance
            balance += deposit
            #balance vannn number 444 bijv. !!
            print "Your new balance is €", balance
        else:
            print "That accountnumber does not exist."
    elif menu_choice == 6:
        print "Quit."
        break
    else:
        print "Please enter a number between 1 and 6."
    print
    print_menu()

# Ook nog ff instellen dat wanneer input geen iets anders is,
# dan gewoon netjes afluisten, geen rare foutcodes !!

【问题讨论】:

  • 我建议采用面向对象的方法来解决这个问题。帐户似乎可以成为一个很棒的对象,并且可以具有数字、名称和余额等属性。这是tutorial 关于如何开始上课的内容。如果您愿意,我可以提供更多建议。
  • Robin,可能他们在他们的课程中还没有达到 Objects。他们正在使用字典、数组等 :) 语言的雏形。
  • JoseM 是对的,我真的还没有接触过 Objects(虽然在互联网上阅读过)。所以我猜学校不会允许我这样提交。

标签: python dictionary account


【解决方案1】:

问题在于您以错误的方式使用“平衡”。如果您使用像字典这样的帐户,其中有“帐号”和“余额”,那就更好了。

如果你有类似这样的东西:

accounts = {1:100, 2:1500, 3:0}

这意味着帐户编号为 1 的人有 100 美元,帐户编号为 2 的第二个人有 1500,依此类推。

例如,在选项 4 中,您正在这样做:

elif menu_choice == 4:
        print "Withdraw money from Account."
        number = input("Accoutnnumber: ")
        if number in accounts:
            withdraw = float(input("How much money would you like to withdraw? > "))
            if withdraw < balance:
                #accounts[number]=balance
                numbers[balance] -= withdraw
                #balance vann number 444 bijv. !!
                print "Your new balance is €", balance
            else:
                print "There are no sufficient funds on this accountnumber"

但这样更好:

elif menu_choice == 4:
        print "Withdraw money from Account."
        number = input("Accoutnnumber: ")
        if number in accounts:
            withdraw = float(input("How much money would you like to withdraw? > "))
            balance = accounts[number]
            if withdraw < balance:
                accounts[number] -=withdraw 
                print "Your new balance is €", accounts[number]
            else:
                print "There are no sufficient funds on this accountnumber"

我希望这对你的作业有所帮助。

【讨论】:

  • 首先感谢您的帮助!然而,问题是,用户应该能够创建一个帐户(例如 444),并且用户创建的帐户应该有自己的余额。所以应该有可能用户首先开了4个账户,即444、333、222、111,然后,例如,在accountnr 444上存入10美元,在accountnr 222上存入50美元。这意味着accountnr 444有10美元的余额, accountnr 222 的余额为 50 美元。而且,用户应该可以通过 List Accounts 查看(当前打开的所有账户都显示有自己的余额。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-26
  • 1970-01-01
  • 2011-04-27
  • 1970-01-01
  • 2016-07-18
相关资源
最近更新 更多