0

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: "jokester.scott.michael@gmail.com."

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.

Blaž
  • 11
  • 1
  • What do you want the dictionary structure to look like? As written, the value associated with the key 'Name and surname' will be a list of strings which may be exactly what you want – DarkKnight May 05 '22 at 17:49
  • For one thing, it's bad practice to name a variable sth like `dict`, as this shadows the built-in `dict()` and can cause unwanted consequences. That aside, what's wrong with your current output? When I run it it appears to do exactly as you described – G. Anderson May 05 '22 at 17:50
  • @G.Anderson thanks for the tip. I need the program to now randomize the separated words. I don't know how to do that, that's what I need the help with. – Blaž May 05 '22 at 18:17
  • That wasn't clear from the question, thanks for clarifying. See [this question and related answers](https://stackoverflow.com/questions/976882/shuffling-a-list-of-objects). TL;DR: Python has a built-in random module, which contains the [random.shuffle](https://docs.python.org/3/library/random.html#random.shuffle) method – G. Anderson May 05 '22 at 19:54

2 Answers2

0

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'
Sueng
  • 59
  • 4
  • Thanks for the reply, but I need it to be a dict and not a list. Is there no way to achieve what I need with dict only? Also I tried your contribution and it didn't work, had an error, am I suppose to change something? – Blaž May 05 '22 at 18:10
  • Dict objects use { key : value } pairs as you know. So it is not possible to code the structure that you want using only dict as far as I know . May be you can create like a structure : ```python sample_dct['name and surname'] = {1:{'name':name_surname[:-1],'surname':name_surname[-1],2:{{'name':name_surname[:-1],'surname':name_surname[-1]...}``` like that – Sueng May 05 '22 at 18:24
0

Is this what you are looking for? You never know what names you are going to get.

dict_data = {}
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_data['name'] = data[0]
        data.pop(0)
        dict_data['last_name'] = data[-1]
        data.pop(-1)
    while arr_size > len(data) != 0:
        name = 'middle_name_' + str(count)
        dict_data[name] = data[0]
        data.pop(0)
        count += 1


name(name_surname)
print(dict_data)
GodDeus
  • 17
  • 7