Email validator: Difference between revisions

From Computer Science Wiki
 
(4 intermediate revisions by one other user not shown)
Line 10: Line 10:
== The Problem ==
== The Problem ==


Please program the following functions:  
You should write a program that asks a user to enter an email address. Your program should check if the email:  


# '''Mean''' For a data set, the terms arithmetic mean, mathematical expectation, and sometimes average are used synonymously to refer to a central value of a discrete set of numbers: specifically, the sum of the values divided by the number of values.<ref>https://en.wikipedia.org/wiki/Mean</ref>
# has an @ sign
#'''Mode'''  The mode is the value that appears most often in a set of data. <ref>https://en.wikipedia.org/wiki/Mode_(statistics)</ref>
# has a period .
# '''Median''' In statistics and probability theory, a median is the number separating the higher half of a data sample, a population, or a probability distribution, from the lower half. The median of a finite list of numbers can be found by arranging all the observations from lowest value to highest value and picking the middle one (e.g., the median of {3, 3, 5, 9, 11} is 5). If there is an even number of observations, then there is no single middle value; the median is then usually defined to be the mean of the two middle values<ref>https://en.wikipedia.org/wiki/Median</ref>
# has a common top level domain (org, com)
 
== Some Code to Get You Started ==
 
<syntaxhighlight lang="python">
 
email = raw_input("Enter your email ")
 
 
</syntaxhighlight>


== Take This Further ==
== Take This Further ==
Line 39: Line 30:
<references />
<references />


== One Possible Solution ==
== A possible solution ==




<div class="toccolours mw-collapsible mw-collapsed">
<div class="toccolours mw-collapsible mw-collapsed">
Click the expand link to see one possible solution, but NOT before you have tried and failed!
Click the expand link to see one possible solution, but NOT before you have tried and failed at least three times
<div class="mw-collapsible-content">
<div class="mw-collapsible-content">
<syntaxhighlight lang="python" >


list=[2,3,3,2,3,2,3,9,7,3,4,8,1,2,8,7,6,5,8,9,1,2,3,2,1,4,3,2,1,4,5,4,1,6,9,6,1,4,2,3,5]
<syntaxhighlight lang="python">


def mean(list):
top_level_domains = ['com', 'org', 'pl', 'kz', 'edu']
    answer = sum(list) 
    mean = answer / len(list)
    return mean


def mode(list):
    frequency = {}
    highest = max(list)
    lowest = min(list)
    # in this loop, we simply update our dictionary named "frequency with the count of values.
    for i in range(lowest,highest+1):
        frequency.update({i:list.count(i)})
    values = frequency.values()
    keys = frequency.keys()
    mode = keys[values.index(max(values))]
    return mode


def median(list):
while True:
     new_list = sorted(list)
     email = raw_input("Enter you email ")
     if len(new_list) % 2 == 1:
     if '@' not in email:
         median = new_list[len(list)/2]
         print("Error: you need an @ sign in your email.")
     return median
        break
 
     elif '.' not in email:
print("the mean of list is: " + str(mean(list)))
        print("Error: you need a . sign in your email. Please try again")   
print("the median of list is: " + str(median(list)))
        break
print("the mode of list is: " + str(mode(list)))
    # I am grateful to my student Sneha for this line below. It is a succinct way of capturing the characters AFTER the period.
    elif email[email.index('.')+1:] not in top_level_domains:
        print("Error: you need a valid domain name at the end")  
    else:
        print("Success: the email address you entered has passed our basic validation tests.")  
        break 


</syntaxhighlight>
</syntaxhighlight>

Latest revision as of 12:41, 13 December 2016

This a problem set for you to work through [1]

This is a problem set. Some of these are easy, others are far more difficult. The purpose of these problems sets are to HELP YOU THINK THROUGH problems. The solution is at the bottom of this page, but please don't look at it until you have tried (and failed) at least three or four times.


What is this problem set trying to do[edit]

You are going to use very simple if not in logic here. If you complete this problem set, you will have shown me you sort of understand how to test a string for a specific character. You will have ALSO shown me that you know how to program a very simple validator.

The Problem[edit]

You should write a program that asks a user to enter an email address. Your program should check if the email:

  1. has an @ sign
  2. has a period .
  3. has a common top level domain (org, com)

Take This Further[edit]

  1. you can test for a @ and a . Could you also test for common misspellings?
  2. if you can test for common misspellings, could you suggest the correct spelling?
  3. we can check for common .com, .org, addresses but what about other top level domain names click here for a fairly scary list of them

How you will be assessed[edit]

Every problem set is a formative assignment. Please click here to see how you will be graded

References[edit]

A possible solution[edit]

Click the expand link to see one possible solution, but NOT before you have tried and failed at least three times

top_level_domains = ['com', 'org', 'pl', 'kz', 'edu']


while True:
    email = raw_input("Enter you email ")
    if '@' not in email:
        print("Error: you need an @ sign in your email.")
        break
    elif '.' not in email:
        print("Error: you need a . sign in your email. Please try again")    
        break
    # I am grateful to my student Sneha for this line below. It is a succinct way of capturing the characters AFTER the period.
    elif email[email.index('.')+1:] not in top_level_domains:
        print("Error: you need a valid domain name at the end")    
    else:
        print("Success: the email address you entered has passed our basic validation tests.") 
        break