Python Forum
How do you replace a dictionary key with a new input?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do you replace a dictionary key with a new input?
#1
Good afternoon Python community,

I am practicing my Python skills by working on a log-in/registration script that asks if you are currently registered. If Y for 'Yes', then it runs the user through a log in validator. If 'No' then it runs the user through a sign-up form that validates each input field, and stores it into a dictionary userBase. At the end of the sign-up form, the program lists out all of the fields that were filled in to verify if the information is correct. If Y for "Yes" the program exits. If "N" for no, the program asks the user to choose which field they want to update, and re-prompts the field for them to update the selected information. This will loop until the user chooses Y for "Yes", and the program will exit.

My problem occurs when the user is asked to update a field. In a perfect world, the updated information will replace the appropriate dictionary key/value. However, I have not wrapped my finger around how exactly can I go about doing this. The original program is about 140 lines long, so I isolated the problem to 14 lines in order for you to see what I'm talking about:

import pprint

username = input('enter username: ')
password = input('Enter a password: ')
userBase = dict(
    user = {
        username:{'password': password}
        })


updateUsername = input('Enter in your new username: ')
userBase['user'][username] == updateUsername

pprint.pprint(userBase)
The output that I'm getting is:
Output:
enter username: John Enter in your new username: Jeff {'user': {'John': {'password':'12345abcde'}}}
The desired output is:
Output:
enter username: John Enter in your new username: Jeff {'user': {'Jeff': {'password':'12345abcde'}}}
I have searched around the internet for strategies on how to replace a dictionary key/value without entering an immutable string value, but nothing has worked for me so far. Is this something that I have to hack? Am I overlooking something that's very simple? Cry
Reply


Messages In This Thread
How do you replace a dictionary key with a new input? - by thewetmosquito - Aug-22-2020, 07:16 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  User input & Dictionary tfernandes 5 3,674 Apr-03-2020, 07:12 PM
Last Post: tfernandes
  Get a value from a dictionary through input Anony 3 2,300 Jan-26-2020, 06:18 PM
Last Post: buran
  Problem using input in Dictionary.. Help roseojha 1 1,794 Aug-25-2019, 08:51 AM
Last Post: ThomasL
  Search & Replace - Newlines Added After Replace dj99 3 3,428 Jul-22-2018, 01:42 PM
Last Post: buran
  QC input value against dictionary in while loop dadgums 2 2,947 Apr-23-2018, 11:38 PM
Last Post: dadgums
  User Input to Choose from Dictionary anelliaf 9 25,903 Mar-27-2018, 02:22 PM
Last Post: anelliaf

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020