import string
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
import random
from random import randint
Number_of_emailname_letters = randint(3,10)
Number_of_emailDomain_letters = randint(5,15)
Email_name = 'pt1'
Email_Domain = 'ptd1'
def Email_name():
if Number_of_emailname_letters == 3:
Email_name = pt1, pt2, pt3
elif Number_of_emailname_letters == 4:
Email_name = pt1, pt2, pt3, pt4
elif Number_of_emailname_letters == 5:
Email_name = pt1, pt2, pt3, pt4, pt5
elif Number_of_emailname_letters == 6:
Email_name = pt1, pt2, pt3, pt4, pt5, pt6
elif Number_of_emailname_letters == 7:
Email_name = pt1,pt2, pt3, pt4, pt5,pt6, pt7
elif Number_of_emailname_letters == 8:
Email_name = pt1, pt2, pt3, pt4, pt5, pt6, pt7, pt8
elif Number_of_emailname_letters == 9:
Email_name = pt1, pt2, pt3, pt4, pt5, pt6, pt7, pt8, pt9,
elif Number_of_emailname_letters == 10:
Email_name = pt1, pt2, pt3, pt4, pt5, pt6, pt7, pt8, pt9, pt10
def Email_Domain():
if Number_of_emailDomain_letters == 5:
Email_Domain = ptd1, ptd2, ptd3, ptd4, ptd5
elif Number_of_emailDomain_letters == 6:
Email_Domain = ptd1, ptd2, ptd3, ptd4, ptd5, ptd6
elif Number_of_emailDomain_letters == 7:
Email_Domain = ptd1, ptd2, ptd3, ptd4, ptd5, ptd6, ptd7
elif Number_of_emailDomain_letters == 8:
Email_Domain = ptd1, ptd2, ptd3, ptd4, ptd5, ptd6, ptd7, ptd8
elif Number_of_emailDomain_letters == 9:
Email_Domain = ptd1, ptd2, ptd3, ptd4, ptd5, ptd6, ptd7, ptd8, ptd9
elif Number_of_emailDomain_letters == 10:
Email_Domain = ptd1, ptd2, ptd3, ptd4, ptd5, ptd6, ptd7, ptd8, ptd9, ptd10
elif Number_of_emailDomain_letters == 11:
Email_Domain = ptd1, ptd2, ptd3, ptd4, ptd5, ptd6, ptd7, ptd8, ptd9, ptd10, ptd11
elif Number_of_emailDomain_letters == 12:
Email_Domain = ptd1, ptd2, ptd3, ptd4, ptd5, ptd6, ptd7, ptd8, ptd9, ptd10, ptd11, ptd12
elif Number_of_emailDomain_letters == 13:
Email_Domain = ptd1, ptd2, ptd3, ptd4, ptd5, ptd6, ptd7, ptd8, ptd9, ptd10, ptd11, ptd12, ptd13
elif Number_of_emailDomain_letters == 14:
Email_Domain = ptd1, ptd2, ptd3, ptd4, ptd5, ptd6, ptd7, ptd8, ptd9, ptd10, ptd11, ptd12, ptd13, ptd14
elif Number_of_emailDomain_letters == 15:
Email_Domain = ptd1, ptd2, ptd3, ptd4, ptd5, ptd6, ptd7, ptd8, ptd9, ptd10, ptd11, ptd12, ptd13, ptd14, ptd15
# Email name generator
pt1 = random.choice(string.ascii_letters)
pt2 = random.choice(string.ascii_letters)
pt3 = random.choice(string.ascii_letters)
pt4 = random.choice(string.ascii_letters)
pt5 = random.choice(string.ascii_letters)
pt6 = random.choice(string.ascii_letters)
pt7 = random.choice(string.ascii_letters)
pt8 = random.choice(string.ascii_letters)
pt9 = random.choice(string.ascii_letters)
pt10 = random.choice(string.ascii_letters)
Email_name()
# Email Domain generator
ptd1 = random.choice(string.ascii_letters)
ptd2 = random.choice(string.ascii_letters)
ptd3 = random.choice(string.ascii_letters)
ptd4 = random.choice(string.ascii_letters)
ptd5 = random.choice(string.ascii_letters)
ptd6 = random.choice(string.ascii_letters)
ptd7 = random.choice(string.ascii_letters)
ptd8 = random.choice(string.ascii_letters)
ptd9 = random.choice(string.ascii_letters)
ptd10 = random.choice(string.ascii_letters)
ptd11 = random.choice(string.ascii_letters)
ptd12 = random.choice(string.ascii_letters)
ptd13 = random.choice(string.ascii_letters)
ptd14 = random.choice(string.ascii_letters)
ptd15 = random.choice(string.ascii_letters)
Email_Domain()
print(Email_name,"@",Email_Domain, ".com")
So, this is my code to create a randomized email and output it. I can't get it all to output in some clean way.I have tried setting a variable to a function, but that didn't get me what i wanted. what i want to happen is get an output something like
[email protected]
and i can't get that. what i got was
<function Email_name at 0x7f53299c7130> @ <function Email_Domain at 0x7f5329861900> .com
and I've tried setting a variable to a function but then i just get something like
'a, S, V, i'
'G, h, k, L,I, t'
none @ none .com
How can i get what i want to get?
CodePudding user response:
I think you are missing a few concepts here. I strongly recommend you get an introductory text that will clear up many things.
This book is free and has excellent coverage of basic topics:
I'd recommend Chapter 3 on functions. And everything else. :)
Regarding your specific problem above, you should write your function to take inputs (rather than rely on global variables) and it needs to return a value if you want to catch it outside the function. Then you will need some variable on the "outside" to catch what is returned by the function. This is a "fruitful function" because it returns some value.
So the "inputs" or parameters of what you want to do is the number of letters. The output is the generated word. So that sets up the function. And anytime you are assigning a bunch of variables in a row, you should be thinking about making a loop of some kind. (That's in the book also! ;) )
Here's an idea:
from random import choice
def name_maker(num_chars):
letters = list('abcdefg')
# an empty list to hold the selected characters:
name_as_list = []
# make a little loop to add randomly selected characters
for i in range(num_chars):
letter = choice(letters)
name_as_list.append(letter)
# join the list into a string...
result = ''.join(name_as_list)
# return the value from the function
return result
my_name = name_maker(9)
print(my_name)
CodePudding user response:
Your problem was that the functions were not returning any values:
Ive fixed your code up to a point where you start to see output that you might expect to see:
import string
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
import random
from random import randint
Number_of_emailname_letters = randint(3,10)
Number_of_emailDomain_letters = randint(5,15)
Email_name = 'pt1'
Email_Domain = 'ptd1'
def Email_name():
if Number_of_emailname_letters == 3:
Email_name = pt1, pt2, pt3
elif Number_of_emailname_letters == 4:
Email_name = pt1, pt2, pt3, pt4
elif Number_of_emailname_letters == 5:
Email_name = pt1, pt2, pt3, pt4, pt5
elif Number_of_emailname_letters == 6:
Email_name = pt1, pt2, pt3, pt4, pt5, pt6
elif Number_of_emailname_letters == 7:
Email_name = pt1,pt2, pt3, pt4, pt5,pt6, pt7
elif Number_of_emailname_letters == 8:
Email_name = pt1, pt2, pt3, pt4, pt5, pt6, pt7, pt8
elif Number_of_emailname_letters == 9:
Email_name = pt1, pt2, pt3, pt4, pt5, pt6, pt7, pt8, pt9,
elif Number_of_emailname_letters == 10:
Email_name = pt1, pt2, pt3, pt4, pt5, pt6, pt7, pt8, pt9, pt10
return Email_name
def Email_Domain():
if Number_of_emailDomain_letters == 5:
Email_Domain = ptd1, ptd2, ptd3, ptd4, ptd5
elif Number_of_emailDomain_letters == 6:
Email_Domain = ptd1, ptd2, ptd3, ptd4, ptd5, ptd6
elif Number_of_emailDomain_letters == 7:
Email_Domain = ptd1, ptd2, ptd3, ptd4, ptd5, ptd6, ptd7
elif Number_of_emailDomain_letters == 8:
Email_Domain = ptd1, ptd2, ptd3, ptd4, ptd5, ptd6, ptd7, ptd8
elif Number_of_emailDomain_letters == 9:
Email_Domain = ptd1, ptd2, ptd3, ptd4, ptd5, ptd6, ptd7, ptd8, ptd9
elif Number_of_emailDomain_letters == 10:
Email_Domain = ptd1, ptd2, ptd3, ptd4, ptd5, ptd6, ptd7, ptd8, ptd9, ptd10
elif Number_of_emailDomain_letters == 11:
Email_Domain = ptd1, ptd2, ptd3, ptd4, ptd5, ptd6, ptd7, ptd8, ptd9, ptd10, ptd11
elif Number_of_emailDomain_letters == 12:
Email_Domain = ptd1, ptd2, ptd3, ptd4, ptd5, ptd6, ptd7, ptd8, ptd9, ptd10, ptd11, ptd12
elif Number_of_emailDomain_letters == 13:
Email_Domain = ptd1, ptd2, ptd3, ptd4, ptd5, ptd6, ptd7, ptd8, ptd9, ptd10, ptd11, ptd12, ptd13
elif Number_of_emailDomain_letters == 14:
Email_Domain = ptd1, ptd2, ptd3, ptd4, ptd5, ptd6, ptd7, ptd8, ptd9, ptd10, ptd11, ptd12, ptd13, ptd14
elif Number_of_emailDomain_letters == 15:
Email_Domain = ptd1, ptd2, ptd3, ptd4, ptd5, ptd6, ptd7, ptd8, ptd9, ptd10, ptd11, ptd12, ptd13, ptd14, ptd15
return Email_Domain
# Email name generator
pt1 = random.choice(string.ascii_letters)
pt2 = random.choice(string.ascii_letters)
pt3 = random.choice(string.ascii_letters)
pt4 = random.choice(string.ascii_letters)
pt5 = random.choice(string.ascii_letters)
pt6 = random.choice(string.ascii_letters)
pt7 = random.choice(string.ascii_letters)
pt8 = random.choice(string.ascii_letters)
pt9 = random.choice(string.ascii_letters)
pt10 = random.choice(string.ascii_letters)
Email_name()
# Email Domain generator
ptd1 = random.choice(string.ascii_letters)
ptd2 = random.choice(string.ascii_letters)
ptd3 = random.choice(string.ascii_letters)
ptd4 = random.choice(string.ascii_letters)
ptd5 = random.choice(string.ascii_letters)
ptd6 = random.choice(string.ascii_letters)
ptd7 = random.choice(string.ascii_letters)
ptd8 = random.choice(string.ascii_letters)
ptd9 = random.choice(string.ascii_letters)
ptd10 = random.choice(string.ascii_letters)
ptd11 = random.choice(string.ascii_letters)
ptd12 = random.choice(string.ascii_letters)
ptd13 = random.choice(string.ascii_letters)
ptd14 = random.choice(string.ascii_letters)
ptd15 = random.choice(string.ascii_letters)
Email_Domain()
name = Email_name()
domain = Email_Domain()
print(name,"@", domain, ".com")
Produces:
python .\SO74380065.py
('w', 'y', 'Q', 'H', 'q', 's', 'n', 'm') @ ('m', 'r', 'P', 'e', 'r', 'E', 'c', 'x', 'k') .com
Your next step will be to contatenate each of the tuple items into a string:
Changing the last line to:
print(''.join(name),"@", ''.join(domain), ".com")
Produces
wMg @ jgsSFHFfSLJu .com
Finally, to get a single string email address, change the last line to:
Changing the last line to:
finaltuple=(''.join(name), "@", ''.join(domain), ".com")
print(''.join(finaltuple))
Produces
[email protected]
Hope that helps!