Python homework Lesson 1 and python Lesson 1

Source: Internet
Author: User

Python homework Lesson 1 and python Lesson 1

I started learning from scratch. Recently, all the students around me are learning. I also want to try it. Hey, I'll write it down. I can come and see it next time I don't remember it ~~

Python homework Lesson 1
1) login. Three inputs are locked. login is not allowed next time
2) Design a three-level menu. The menu content can be customized. If q is input at any level, exit the program. If B is input, return to the upper-level menu.

-- The above two questions involve several knowledge points: reading documents, writing documents, list operations, recycling, and string operations.
First, let's review these knowledge points.
A) reading documents, several common
F = open ("test. log", "w ")
This w is a parameter and can be changed to another parameter.
W open in write mode,
A is opened in append mode (starting from EOF and creating a file if necessary)
R + Enabled in read/write mode
W + is enabled in read/write mode (see w)
A + is enabled in read/write mode (see)
B) close the file.
F. close ()
C) File Content operations
F. read () reads all the content at a time, and can be adjusted to f. read ([size]) to read the size of the content in bytes.
F. readline () reads a row of data
F. readlines () reads all the content at a time and returns the result by row.
F. truncate (n) truncates n characters from the beginning of the file and deletes the files that exceed the limit.
F. seek (n) jumps to the specified position n, and f. seek (0) is the start of the returned file.
E.g.
H = open ("menu2.log", "r ")
For line in h. readlines ():
If "2" in line:
Print (line)
H. close ()
F. write (str) write File
E.g. A new file is created, and the previous content will be cleared.
H = open ("menu2.log", "w ")
H. write ("this is the test line ")
H. close ()
E.g. append after the previous file
H = open ("menu2.log", "")
H. write ("this is the add line ")
H. close ()
D) list operations (list)
Name_list = ["a", "B", "c", "d"]
* ******************************** Here is a bit interspersed
We can use dir (name_list) to find executable commands. [Note that the front and back _ lines are useless and do not need to be viewed.]
Type (name_list) can return the type of this variable
**************************************** ********************
Slice: (starting from 0, negative starting from the right,-1 is the rightmost value)
Name_list [1]> B
Name_list [-1]> d
Name_list [0: 2]> a, B 0: 2 indicates that 01 values do not include 2
Name_list []> a, c, the front is the one after abc, and the two is the one after the two and then the cut
Name_list.append ("e")> a, B, c, d, and e add an element to the end of the list.
Name_list.pop () deletes the last element of the list.
Name_list.remove ("c") deletes an element called c.
Name_list [1] = "ff" change the second element to ff
Name_list.insert (1, "test") inserts a new element with an index of 1.
Name_list.count ("ff") ff count
List2 = ["ee", "gg"]
Name_list.extend (list2) combines list2 to name_list
Name_list.sort () sorts the list
E) tuple)
The tuples are similar to the list. The difference is that once the initialization is complete, no append or insert methods are available.
F) string formatting
Name = input ("name:"). strip () input the name and remove left and right Blank
Print (name) Output name
Print (name, + "-" + name)
Print ("Name: % s: \ nAge: % s \ nJob: % s" % (name, age, job ))
G) loop
For I in range (3): from 0-2

While a = 0:
While true: -- infinite loop

Break bounce cycle
Continue
H) condition judgment
If a = 0:
Elif a = 1:
Else:

This is the basic knowledge point.

1) login. Three inputs are locked. login is not allowed next time
First, two files are prepared. One is the user and the other is the user whose password is written to the lock. The two files are named user. log and lockuser. log respectively.
The content in user. log is prepared by myself in advance, as shown below:
Autumn, autumn123
Summer, summer123
Angle, angle123
Tiffany, 2017any123
Fay, fay123
Lockuser. log is empty at the beginning.
If the user name is entered correctly and the password is entered incorrectly three times, it is written to lockuser. log. The next time you log on with this user, the user will be prompted to be locked.

The Code is as follows:

Input_name = input ("The name is :"). strip () f = open ("lockuser. log "," r ") count = 0 # identify whether the account is locked count1 = 0 # identify whether to lose the account # view the file to see if the entered account is locked, lock to exit the program for line in f. readlines (): if line. strip () = input_name: print ("The account has locked! ") Count = 1 breakf. close () # if the account is not locked, if count = 0: f1 = open ("user. log "," r ") for line in f1.readlines (): lineword = line. split (',', 2) if lineword [0] = input_name: count1 = 1 # enter the account correctly. Enter the password three times and enter for I in range (3): input_password = input ("The password is :"). strip () if input_password = lineword [1]. strip (): print ("Welcome to here, have a good nice trip! ") Break if I = 2: f2 = open (" lockuser. log "," a ") f2.write (" \ n ") f2.write (lineword [0]) f2.close () print (" Three times, the account has locked! ") If count1 = 0: print (" The user account is not exits! ")

 

2) Design a three-level menu. The menu content can be customized. If q is input at any level, exit the program. If B is input, return to the upper-level menu.

First, I use constants to write SHI in the Code. Second-level menus and third-level menus are placed in two files, named menu2.log and menu3.log respectively, of course, the level-1 menu can also be written to a file to read the file.
The focus of this Code is that in several layers of loops, all the variables are substituted into a count variable to jump out of the outermost loop (when q is input)

When all inputs are normal, the program will finally output the three options you have selected, for example, Beijing-Chaoyang-Sanlitun Street.
The Code is as follows:

Count1 = 0count2 = 0count3 = 0input_name1 = "" input_name2 = "" input_name3 = "" value1 = "" value2 = "" value3 = "" print ("Choose the favorite place! ") While count1 = 0: # select the first-level menu print ("****************************** ********************") print ("1 Beijing") print ("2 Shanghai") print ("3 Guangzhou") print ("4 Shenzhen ") print ("************************************* *************") input_name = input ("Please chose the fire level :"). strip () if input_name = "1": value1 = "Beijing" elif input_name = "2": value1 = "Shanghai" elif input_name = "3 ": value1 = "Guangzhou" elif input_name = "4": value1 = "Shenzhen" # Read the file according to the option of the first menu to display the second menu. If yes, the second menu is displayed, prompt to select Level 2 menu if input_name in ('1', '2', '3', '4'): f = open ("menu2.log", "r ", encoding = 'utf-8') for line in f. readlines (): if input_name in line: lineword = line. split (',', 4) for I in range (len (lineword): if I> 0: print (str (I) + "" + lineword [I]) f. close () print ("************************************* *************") # print (value1) input_name2 = Indium Ut ("Please chose the second level :"). strip () input_name1 = input_name + input_name2 # If B is input, elif input_name = "B ": continue # exit the entire program if q is used. elif input_name = "q": print ("See you next time! ") Break # If other characters are entered, the system also exits and prompts invalid else: print (" Valid input! Please try again! ") Continue while count2 = 0: # display Level 3 menus Based on Level 1 menus # if the input menu is within the valid range, read Level 3 menus if input_name2 in ('1 ', '2', '3'): value2 = lineword [int (input_name2)] # print (value2) g = open ("menu3.log", "r ", encoding = "UTF-8") for line in g. readlines (): if input_name1 in line: lineword = line. split (',', 4) for I in range (len (lineword): if I> 0: print (str (I) + "" + lineword [I]) g. close () print ("****************************** * ****************** ") Input_name3 = input (" Please chose the third level :"). strip () # If you enter B to exit the loop, return to the upper menu elif input_name2 = "B": break # If you enter q, exit the entire loop elif input_name2 = "q ": count1 = 1 print ("See you next time! ") Break # If other characters are entered, the system also exits and prompts invalid else: print (" Valid input! Please try again! ") Break # determination of the third commit while count3 = 0: if input_name3 =" q ": count1 = 1 count2 = 1 print (" See you next time! ") Break elif input_name3 =" B ": break elif input_name3 in ('1', '2', '3'): value3 = lineword [int (input_name3)] print ("Your favorite place is:" + value1 + "-" + value2 + "-" + value3) count1 = 1 count2 = 1 break else: print ("Valid input! Please try again! ") Break

 

The flowchart is not shown. Next time I will draw a flowchart. It may be so many things that I will continue to study tomorrow. It's just a bit oily !!!

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.