In this assignment, you will implement an online banking system. Users can sign-up with the system, log in to the system, change their password, and delete their account. They can also update their bank account balance and transfer money to another user’s bank account.
You’ll implement functions related to File I/O and dictionaries. The first two functions require you to import files and create dictionaries. User information will be imported from the "users.txt" file and account information will be imported from the "bank.txt" file. Take a look at the content in the different files. The remaining functions require you to use or modify the two dictionaries created from the files.
Each function has been defined for you, but without the code. See the docstring in each function for instructions on what the function is supposed to do and how to write the code. It should be clear enough. In some cases, we have provided hints to help you get started.
def import_and_create_dictionary(filename):
'''
This function is used to create a bank dictionary. The given argument is the filename to load.
Every line in the file will look like
key: value
Key is a user's name and value is an amount to update the user's bank account with. The value should be a
number, however, it is possible that there is no value or that the value is an invalid number.
What you will do:
- Try to make a dictionary from the contents of the file.
- If the key doesn't exist, create a new key:value pair.
- If the key does exist, increment its value with the amount.
- You should also handle cases when the value is invalid. If so, ignore that line and don't update the dictionary.
- Finally, return the dictionary.
Note: All of the users in the bank file are in the user account file.
'''
d = {}
# your code here
return d
##########################
### TEST YOUR SOLUTION ###
##########################
bank = import_and_create_dictionary("bank.txt")
tools.assert_false(len(bank) == 0)
tools.assert_almost_equal(115.5, bank.get("Brandon"))
tools.assert_almost_equal(128.87, bank.get("James"))
tools.assert_is_none(bank.get("Joel"))
tools.assert_is_none(bank.get("Luke"))
tools.assert_almost_equal(bank.get("Sarah"), 827.43)
#user.txt
Brandon - brandon123ABC
Jack
Jack - jac123
Jack - jack123POU
Patrick - patrick5678
Brandon - brandon123ABCD
James - 100jamesABD
Sarah - sd896ssfJJH
Jennie - sadsaca
#bank.txt
Brandon: 5
Patrick: 18.9
Brandon: xyz
Jack:
Sarah: 825
Jack : 45
Brandon: 10
James: 3.25
James: 125.62
Sarah: 2.43
Brandon: 100.5

Respuesta :

Answer:

# global variable for logged in users

loggedin = []

# create the user account list as a dictionary called "bank".

def import_and_create_dictionary(filename):

   bank = {}

   i = 1

   with open(filename, "r") as file:

       for line in file:

           (name, amt) = line.rstrip('\n').split(":")

           bank[i] = [name.strip(), amt]

           i += 1

   return bank

#create the list of user login details.

def import_and_create_accounts(filename):

   acc = []

   with open(filename, "r") as file:

       for line in file:

           (login, password) = line.rstrip('\n').split("-")

           acc.append([login.strip(), password.strip()])

   return acc

# function to login users that are yet to login.

def signin(accounts, login, password):

   flag="true"

   for lst in accounts:

       if(lst[0]==login and lst[1] == password):

           for log in loggedin:

               if(log ==login):

                    print("You are already logged in")

                    flag="false"

           break                    

       else:

           flag="false"  

   if(flag=="true"):

       loggedin.append(login) # adding user to loggedin list

       print("logged in succesfully")

       return True

   return False

# calling both the function to create bank dictionary and user_account list        

bank  = import_and_create_dictionary("bank.txt")            

print (bank)

user_account = import_and_create_accounts("user.txt")

print(user_account)

# check for users loggedin

signin(user_account,"Brandon","123abcAB")

signin(user_account,"James","100jamesABD")

signin(user_account,"Sarah","sd896ssfJJH")

Explanation:

The python program is a banking application system that creates a list of user accounts and their login details from the bank.txt and user.txt file. The program also uses the information to log in and check for users already logged into their account with the "loggedin" global variable.

In this exercise we have to use the knowledge of programming in the python language, in this way we find that the code can be written as:

The code can be seen in the attached image.

To make it even simpler to visualize the code, we can rewrite it below as:

def import_and_create_dictionary(filename):

  bank = {}

  i = 1

  with open(filename, "r") as file:

      for line in file:

          (name, amt) = line.rstrip('\n').split(":")

          bank[i] = [name.strip(), amt]

          i += 1

  return bank

#create the list of user login details.

def import_and_create_accounts(filename):

  acc = []

  with open(filename, "r") as file:

      for line in file:

          (login, password) = line.rstrip('\n').split("-")

          acc.append([login.strip(), password.strip()])

  return acc

def signin(accounts, login, password):

  flag="true"

  for lst in accounts:

      if(lst[0]==login and lst[1] == password):

          for log in loggedin:

              if(log ==login):

                   print("You are already logged in")

                   flag="false"

          break                  

      else:

          flag="false"  

  if(flag=="true"):

      loggedin.append(login) # adding user to loggedin list

      print("logged in succesfully")

      return True

  return False

bank  = import_and_create_dictionary("bank.txt")      

print (bank)

user_account = import_and_create_accounts("user.txt")

print(user_account)

signin(user_account,"Brandon","123abcAB")

signin(user_account,"James","100jamesABD")

signin(user_account,"Sarah","sd896ssfJJH")

See more about programming at brainly.com/question/11288081

Ver imagen lhmarianateixeira