0

Hello I'm not too sure how to word this, but I'm a beginner in python so I know my code will look really rough but its what I've found from trial and error to work for me. So onto my question, I'm trying to build a mock bank interface this is what the main menu looks like:

#!/usr/bin/env python from time import sleep from CustomerCreator import CreateCustomer users = {} def main(): value = 0 while value == 0: print("Hello and Thank You for Choosing to Bank With the Best!\n\t\tYour Service is Appreciated!") sleep(0.5); print("Please choose one of the following: " "\nCreate New User: 1" "\nManage Your Account: 2" "\nTerminate Your Account: 3" "\nExit Application: 0") sleep(0.25); selection = int(input("Your Selection: ")) value += selection if value == 1: print("Welcome To The New User Menu") CreateCustomer.create_user() # make a class to reset the user back to the main menu if value == 2: input("Enter Account Number or Name to Access: ") # make a class to reset the user back to the main menu if value == 2: test = input("Enter Account Name to Terminate: ") input("Are You Sure You Want to Terminate " + test + "?") # make a class to reset the user back to the main menu main() 

You can see that in the beginning of the code i declare a dictionary in which to store the users, I've already come up with code that will ask for the user to input a name for their bank account and it'll generate a random "account number" to assign the user, the code is as follows:

#!/usr/bin/env python from time import sleep import random class CreateCustomer: @staticmethod def create_user(): def new_user() -> str: print("Welcome to The 'Create New User' Interface") sleep(0.5) x = input("Enter Name to Use for Account Access\n*Name is Case Sensitive to Access Account*: ") print(x) return x def account_generator() -> int: x = '0123456789' acctlen = 8 acctnum = ''.join(random.sample(x, acctlen)) print(acctnum) return int(acctnum) user = new_user() account_number = account_generator() return user, account_number 

Now my question is how can I update the dictionary I declared in the beginning of the main function with the randomly generated "account number" as the key and the account name as the value?

2
  • Have you tried users[account_number]=user? Commented Aug 24, 2018 at 20:42
  • 1
    There doesn't seem to be any reason to have a CreateCustomer class here. Python is not Java; if you only have static methods, just make them standalone functions in a module. Commented Aug 24, 2018 at 21:09

2 Answers 2

2

You can accept the return values of your create_user() method and store them:

# snipped some of your code in def main(): if value == 1: print("Welcome To The New User Menu") user, accountNr = CreateCustomer.create_user() # capture returns into variables users[accountNr] = user # store them inside your dict 
Sign up to request clarification or add additional context in comments.

Comments

2

You can set dictionary values using the dict[key] = value syntax. So based on the values returned from your create_user function you could do something similar to this:

if value == 1: print("Welcome To The New User Menu") new_user, new_account_num = CreateCustomer.create_user() users[new_user] = new_account_num 

I would suggest adding some checking in the create_user function to make sure this new_user does not exist yet as well.

4 Comments

I wanted to do that as well, i wanted to make sort of memory list for the account numbers generated to ensure that no two numbers correlate to one person or to avoid further conflicts in the code
Well your users dictionary provides that list of all created account numbers. So you could pass in the users as a parameter to the create_user() function to check if the newly created number is already in that dictionary.
That's one way to do it, but i would like to check if the user already exists before updating the users dictionary in the main file, so im thinking i would have to add a list to the new_user() function within the CreateCustomer class and check to see if the user inputted name already has been used '
Yes that is what I described in my comment. After the user enters in an account name in the new_user() function you would do a if x in users which checks the users dict if that user key already exists. This list you keep mentioning would just be the users dict you can pass to the function as a parameter.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.