Environment for Python3
1. String and numeric types can be output directly, values need to be added single or double quotation marks
>>> Print (1)
1
>>> print ("Hello World")
Hello World
2.if Judgment Statement
if (1==1) #判断条件:
(print (1)) #执行语句
Else
Print (2) #执行语句
3.json Operation Dict and JSON mutual transfer
D = {"YY": 3, "QW": 0}
Json.dump (D,open ("./json", "W")) #写入
d= json.load (open "./json", "R") #读取
4. Dictionaries, lists
>>>list1 = [three-in-one] #列表是元素的有序集合, each element corresponds to the corresponding table, starting from zero
>>>print List[0]
1
dic= {"One": 1, "a": 2} The dictionary is an unordered combination, you can find the corresponding value by key
>>>print dic["one"]
1
5. Get user input from the console and save as a variable
>>> username = input ("Please enter your user")
Please enter your user Wyx
>>> Print (username)
Wyx
Example: User input three times if the password input error is locked:
#/usr/bin/env python
#coding: Utf-8
Import OS
Import Sys
Import Getpass
Import JSON
Storage_user_file = "./username.py" #保存用户名密码的文件
Correct_user = "Jerry" #正确的用户名
Correct_pass = "Big_jerry" #正确的密码
For I in range (3):
Existing_users_dict = json.load (open (Storage_user_file, ' R ')) #读取文件里面的用户名和对应的错误次数 and convert JSON to dictionary
Get_user = input ("Please your username") #获取用户输入的用户
Get_pass = input ("Please your password") #获取用户输入的密码
if Correct_user = = Get_user and Correct_pass = = Get_pass: #如果用户输入正确的用户名和密码就确认登录
Print ("Welcome to login..")
Break
Else
If Get_user in existing_users_dict: #否则如果错误的用户名次数大于三就输出被锁定
If Existing_users_dict[get_user] >= 3:
Print ("You are locked")
Break
Else
Existing_users_dict[get_user]+=1 #读取文件里面的用户和所对应的次数如果不大于三就给错误的用户名加1
Json.dump (Existing_users_dict,open (Storage_user_file, "w"))
Print ("The user name or password entered is incorrect.")
Else
Existing_users_dict[get_user] = 1 #如果用户名不存在存放用户名的文件, add the wrong username
Json.dump (Existing_users_dict,open (Storage_user_file, "w"))
Print ("The user name or password entered is incorrect.")
The basic application of Python learning