Automatically adding something to a list?


#1

So I'm trying something out on my own kind of like a project.

Here's my code

print "This is an email test and address test\n"
email=raw_input("Please type in your email: ")
confirmation=raw_input("Is this really your email?")
if confirmation == "Yes" or confirmation== "yes":
print "thank you %s, now on to the second question: " % (email)
else
confirmation1=raw_input("Please type in your email again: ")
print "Thank you %s" % (confirmation1)

I want to add an empty list called email_lists=[] that automatically adds the email that the user has entered to the empty list. How do I do that?

Thank you!


#2

The operand on the right side of or is a truthy value so this expression will always yield True.

if confirmation == "Yes" or confirmation == "yes":

or simply,

if confirmatin.lower() == "yes":

To create an empty list, declare it in the first line of your program (above the first print).

emails = []

Now inside the if statement's True branch,

emails.append(email)

Rather than ask for input a second time, it might be better to write a loop that only ends when the user enters "yes" to the question.


#3

But what if what the user entered isn't his email? You told me that I should write a loop that ends only when the user enters "yes", but what if he didn't actually write his true email?
Thanks


#4

You're asking the user to enter an email address, and then you are asking them to confirm that it is their email address. Their yes answer just has to be trusted. Of course programmers live by the mantra, "Never trust the user." This suggests that our program should be confirming that the input from the user is a valid email address pattern. I'll leave you to read and study up on validation.

while True:
    email = raw_input("Enter your email address")
    check = "Is %s your correct email?" % email
    if raw_input(check).lower() == "yes": break

#5

I see! I have to thank you for that. Also, do you know why my "@" is not working when typing an email address? Do I have to import something?


#6

Typing where? At the raw_input() prompt?

     >  
     Enter your email address roy@example.com
     Is roy@example.com your correct email? yes
     > emails
    => ['roy@example.com']

#7

Yes, in the raw_input() prompt. On my PC if I want to use the @ symbol, I have to keep pressing ALT GR + 2 maybe Python doesn't recognize ALT GR?


#8

Does your keyboard not have an @ symbol on the 2 numeral key? Does it have a numeric keypad? Can you use Alt + 64 to insert @?


#9

Yes, everything works out now, thanks!


#10

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.