Happy Valentine's Day, Python help to sow dog food, I did this program overnight!

Source: Internet
Author: User

It takes about 5 minutes to read this article

Code Farm Valentine's Day

The annual lover SectionTo come, this romantic warm festival, walking in the streets and alleys, walking in the subway, walking in the mall, walking in the cinema, the elder woman hands almost all holding a bouquet of flowers, the heart is Lezizi, a face happy appearance, suddenly think of many years ago when I first send flowers, well flash are more than 10 years passed, In such a special day, I also gather a lively, also to participate in this Valentine's Day, so I write a python blind mate's small program, is a code farming unique way to celebrate Valentine's Day ~ ~

Code farming This group, high education, high wages, high IQ, but not good communication, circle is very small, I believe many small partners or around the small partners, have a blind date experience, or parents forced to go blind date experience, is generally asked about age, you want to find what, some also special attention to the Zodiac match, remember that there is a satin Say the male 20-60-year-old only like one type of girl, and 20 to 60 years old girl Blind Date demand is constantly changing, good our small program to achieve such a function

Function:

You enter your gender, age, Birthday, my program to match your favorite type, below we specifically say how to write in Python

Part I: User input section

1. Get the user's input: we use Raw_input ()

Args =raw_input (' Please input your info:sex[man,woman],age[20..60],birthday[month-day]\n ')

We want users to enter 3 parameters, male or female, age between 20-60 years old, birthday, this has 3 questions to solve

1). Check the user's input, first the length

Len (Args.split ()) to determine if it is 3 parameters

2). If the length is right, check whether the first parameter is man or woman

Sexs = [' Man ', ' woman ']

Args.split () [0] not in Sexs:

3). If the first parameter is also right, check the second parameter

Int (Args.split () [1]) < or int (Args.split () [1]) > 60

You can also use INT (Args.split () [1]) not in range (20,61)

One to note, because the input is a string, so for example, the input of 20, is actually the string ' 20 ', must be strong conversion to 20 to compare, and the range value accepts an integer

4). If the second parameter is read, check the third parameter, which is the date

In fact, the legality of the examination needs to use Datetime.datetime.strptime () If successful returns true, otherwise it will error, and then we use try/except to capture, if caught in return false.

My side is still very gentle with if/elif/else such a judgment, of course you can also be violent with raise typeerror this throws some mistakes.

Part Two: The mapping of birthdays and constellations

2. We let the user enter the gender, age and birthday, we need to convert the birthday into constellation, and then the Better call

1). Birthdays and constellations of mapping this is simple, construct 2 tables

Constellation_name_list = [u ' capricorn ', U ' aquarius ', U ' pisces ', U ' Aries ',

U ' taurus ', U ' gemini ', U ' cancer ', U ' Leo ',

U ' virgo ', u ' libra ', U ' scorpio ', U ' sagittarius '

Constellation_date_range = [(1,20), (2,19), (3,21), (4,21),

(5,21), (6,22), (7,23), (8,23),

(9,23), (10,23), (11,22), (12,22)]

2) to determine which interval the birthday falls on

There are many kinds of this solution, there is a relatively flattering solution, is a Python-based list comparison, such as (2,1) < (2,19), with this, plus the filter function, only 1 lines of code to do, this is very clever

Res=filter (lambdaeach_constellation:each_constellation <= (month,day), Constellation_date_range)

will return a filtered sequence, such as the input of the birthday in 3.10, then (3,10) than (3,21) small, so that the filter to get a list is [(1,20), (2,19)], take its length of 2, and then 2 as the Constellation List index, take Constellation_ NAME_LIST[2] It's OK.

Return Constellation_name_list[len (res)%12]

One thing to note is that the constellation's list length is 12, but the subscript is the maximum [11]

Part III: Male and female spouse sample

3. I use a simple network to construct a sample, all say that the male from 20-60 years old has been like a type is "beautiful young", and girls like the object, with their own age and reading growth, demand is constantly changing

25-year-old girl likes = = tall, handsome

30-year-old girls like to have a career success, unrestrained multi-gold

35-year-old women like to be mature and stable, responsible

40-year-old women like to have a mild personality, a common goal of life

Women around the age of 50 like to be healthy and take care of the family.

Women around the age of 60 like to have the same topic, said the

25-year-old boys like to be young and beautiful

30-year-old men like to be young and beautiful

35-year-old men like to be young and beautiful

40-year-old men like to be young and beautiful

Men around the age of 50 like to be young and beautiful

Men around the age of 60 like to be young and beautiful

Sure enough, the man is very professional ~ haha

1). We construct 2 functions

A man to deal with the needs of the mate, a woman to deal with the needs of the spouse, the program is the algorithm + data structure, our side of the data structure with a dictionary, the list can be, I am using a list to construct a

woman_choice_25 = [u ' handsome ', U ' Tall ']

woman_choice_30 = [u ' rich ', U ' business success ']

woman_choice_35 = [u ' sedate ', u ' sense of responsibility ']

Woman_choice_40 = [u ' Good for me ', U ' have a common life goal ']

Woman_choice_50 = [u ' health ', U ' gu ']

woman_choice_60 = [u ' has common language ']

Woman_chocies = [Woman_choice_25,woman_choice_30,woman_choice_35,

WOMAN_CHOICE_40,WOMAN_CHOICE_50,WOMAN_CHOICE_60]

Woman_ages = [25,30,35,40,50,60]

2). Logical section

The entrance must be their own age, the internal logic is very simple, a judgment of ages can be

But it's a bit of a hassle, I used a filter function that encapsulates the judgment of age into the filter and looks a little simpler filter (lambda Age:age < your_age, woman_ages)

Part IV: Matching of male and female constellations

4. Constellation matching Online has a mature mapping relationship, we directly write dead in a dictionary, of course, Dictionary set dictionary

Constellation_mapping_dict = {u ' aries ': [u ' Leo ', U ' aries ', U ' Taurus '],

U ' taurus ': [u ' virgo ', U ' capricorn ', U ' cancer '],

U ' gemini ': [U ' aquarius ', U ' sagittarius ', U ' Libra '],

U ' cancer ': [u ' pisces ', U ' scorpio ', U ' Capricorn '],

U ' Leo ': [u ' sagittarius ', U ' aries ', U ' Aquarius '],

U ' virgo ': [u ' capricorn ', U ' taurus ', U ' pisces '],

U ' libra ': [u ' Gemini ', U ' aquarius ', U ' Leo '],

U ' scorpio ': [u ' pisces ', U ' virgo ', U ' Sagittarius '],

U ' sagittarius ': [u ' aries ', U ' Leo ', U ' Gemini '],

U ' capricorn ': [u ' Taurus ', U ' virgo ', U ' pisces '],

U ' aquarius ': [u ' libra ', U ' gemini ', U ' Leo '],

U ' pisces ': [u ' Scorpio ', U ' cancer ', U ' capricorn ']}

We just need to enter a constellation, and then we can get a matching constellation by looking at the table.

Part V: Interactive output of the program (Chinese and English version)

5. General program output is not only, terminal printing, file output, GUI, Web page

1). We are a small program, simply print out

2). In order to make the user a better physical examination, I use two versions in Chinese and English

We must pay attention to the output of Chinese, you must use the following two lines

Reload (SYS)

Sys.setdefaultencoding (' Utf-8 ')

3). User interaction

I use a while to hold the dead loop, the inside of the continuous printing output results, and then prompted some information, the user according to the prompt, enter the appropriate information, if Q will exit

Well, last look at the results:

Summarize:

In fact, this program is relatively simple, but this topic is actually very interesting, really want to research and excavation of the words, need to use reptiles to the dating network crawl thousands of tens of thousands of samples of data, and then after the data cleaning, stored in the database, and then the data extracted eigenvalues, with some data mining, machine learning algorithms for analysis, And then draw some intuitive histogram, pie chart, so just fun ~ ~ Haha, rest assured that we will give an example of data analysis

Happy Valentine's Day, Python help to sow dog food, I did this program overnight!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.