Python_day1 basic syntax and python_day1 syntax

Source: Internet
Author: User

Python_day1 basic syntax and python_day1 syntax

1. Basic syntax
Variable: name of the custom input variable on the left side. You can enter any type on the right side and assign it to the left side. If you need to specify a type, you can convert it
Name = Jason, age = int (24) provience = ['beijing', 'shanghai', 'shanghai']...

Input: input () accept = input ("prompt content ")

Output: print () Example: print (name, age, province)

2. conditional statements
If condition 1:
Run 1
Elif condition 2:
Run 2
Else:
Run 3

3. Loop For and While
Provience = ['hebei', 'tianjin', 'beijing']
For I in range (10) loop 10 times
For letter in provience: letter is an internal element.

While true:
Pass
Execute when conditions are set.

4. Read and Write files
To open a file, you must have the corresponding permission take_file = open ('filename ', 'authorization ')

You must enable each for loop before reading it !!!

Write: open ('filename ', 'Write authorization'). write ("written content ")
Close open ('filename ', 'Write authorization'). close ()

Permission introduction:
The rU or Ua is opened in Read mode and supports common line breaks (PEP 278)
W open in write mode (new file)
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)
Rb is enabled in binary read mode.
Wb is enabled in binary write mode (see w)
AB is enabled in binary append mode (see)
Rb + is enabled in binary read/write mode (see r +)
Wb + is enabled in binary read/write mode (see w +)
AB + is enabled in binary read/write mode (see a +)
Note: -->
1. Use 'W'. If the file exists, clear the file and create it again,

2. Use the 'A' mode to append all the data to be written to the file to the end of the file, even if you use seek () to point to other parts of the file,
If the file does not exist, it is automatically created.


Homework:
User Login, three consecutive locks
Flag = True
Lock_usr_list = []
Regist_usr = "jason" # define the user name
Regist_pwd = "123" # define password
Login_times = 0 # log on Times

#1. first obtain the Locked User Name and store it to the list
With open ('lock _ user_name ', 'R') as f1:
For lines in f1:
Lock_usr_list.append (lines. strip ())
Print (lock_usr_list)

While flag:
#2. Receive user name and password
Inp_usr = input ("Enter the User name :")
Inp_pwd = input ("enter the password :")

#3. Judgment
#3.1 first determine whether the user is locked
If inp_usr in lock_usr_list:
Print ("User locked ")
Break
#3.2 then determine whether the user name and password are correct
Elif inp_usr = regist_usr and inp_pwd = regist_pwd:
Print ("Logon successful ")
Break
#3.3 otherwise logon fails
Else:
Print ("Logon Failed ")
Login_times + = 1
#3.4 If logon fails three times in a row, the user name is added to the 'blacklist' and the user is logged out.
If login_times> 2:
Print ("account locked ")
With open ('lock _ user_name ', 'a +') as f1:
F1.write (inp_usr + '\ n ')
Break

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.