Home > Software engineering >  How to save input in dictionary (but separate the words when there's a space)?
How to save input in dictionary (but separate the words when there's a space)?

Time:05-06

dict = {}
name_surname = input("Enter your name and surname: ").split(" ")
dict["Name and surname"] = name_surname
print(dict)

I need to make it so that when the user inputs their name and surname (example: Michael Jokester Scott), it will separate the name and the username, so I can use each of them later.

The purpose of this is to be able to take a randomized combinations of someones name and surname(s) and append a "@gmail.com" at the end. This way you get "randomized," but personal email address. So in the end, I should be able to make a randomized email such as: "[email protected]."

What I have so far is pretty bad, I'm new to Python and I don't really understand dict well, lists are easier for me, but I need to learn this as well.

CodePudding user response:

if i understood the problem correctly, you can use lists in a dict.

sample_dct['Name and Surname'] = []
# take input from user
name_surname_list = taken_data_from_user.split()
sample_dct['Name and Surname'].append(name_surname_list)
# get sample_dct values, iterate on these with a loop
# generate 2 random number range between (0,len(sample_dct)) use generated these random numbers to take random value. 
# for surname, use [-1] index and store random_surname; for name, use [:-1] and store random_name.
random_name_full = '.'.join(random_name)
random_mail = '.'.join(random_name_full ,random_surname)   '@gmail.com'

CodePudding user response:

Is this what you are looking for?

dict = {}
name_surname = input("Enter your name and surname: ").split(" ")
arr_size = len(name_surname)


def name(data):
    count = 1
    if arr_size == len(data):
        dict['name'] = data[0]
        data.pop(0)
        dict['last_name'] = data[-1]
        data.pop(-1)
    while arr_size > len(data) != 0:
        print("first: ", len(data))
        name = 'middle_name_'   str(count)
        dict[name] = data[0]
        data.pop(0)
        count  = 1


name(name_surname)
print(dict)
  • Related