Python basic data types and Process Control applications

Source: Internet
Author: User

#2017-05-05 22:10:10
One, user login and failure to lock the account
============================================
1. file reading and writing
2. List Split method
3.while Cycle
============================================

##############################
#datafile文件内容
#larry |123123|1
#linzj |123123|0
##############################
#!/usr/bin/env python
#-*-Coding:utf-8-*-

# Open File Read user information
F1=open (' datafile ', ' R ')
Data=f1.read ()
F1.close ()

# User information reproduced in user information list
User_info_list=[] # Initialize User_info_list
User_str_list=data.split (' \ n ')
# Print (user_str_list)
For item in User_str_list:
Temp=item.split (' | ')
v={' name ': temp[0],
' pwd ': temp[1],
' Times ': temp[2]
}
if int (v[' Times '])!=3: # Resets the non-locked account login information
V[' times '] = 0
User_info_list.append (v)
Print (user_info_list)

# Login Verification
I=0
J=1
While J==1:
V_user_name = input (' User name: ')
V_pwd = input (' Password: ')
For item in User_info_list:
If item[' name '] = = v_user_name and int (item[' Times ') <3:
If item[' pwd '] = = V_pwd:
j=2 # Login successful, set the loop exit
Print (' Welcome back ' +v_user_name)
Else
i = int (item[' Times ')
i + = 1
Item[' times '] = I
Print (item)
Print (user_info_list)
Continue
Elif item[' name '] = = v_user_name and int (item[' Times ']) >=3:
Print (v_user_name+ ' is locked ')
Else
Continue
Print (user_info_list)

# Assign log information of user login to target variable
K=0
Target= "
For item in User_info_list:
Print (item[' name '])
If target!= ':
target=target+ ' \ n '
#target. Append (item[' name ']+ ' | ' +item[' pwd ']+ ' | ' +str (item[' Times '))
target=target+item[' name ']+ ' | ' +item[' pwd ']+ ' | ' +str (item[' Times ')
Print (target)

# Write the target variable value to the file
F2=open (' datafile ', ' W ')
F2.write (target)
F2.close ()

ii. Classification of elements
============================================
1. List Append methods
2.for Cycle
3.if Process Control
============================================

ELEMENT_LIST=[11,22,33,44,55,66,77,88,99]
K1=[]
K2=[]
For item in Element_list:
#print (item)
If item<66:
K1.append (item)
Else
K2.append (item)

Print (K1,K2)
v_dict={' K1 ': K1, ' K2 ': K2}
Print (v_dict)

Third, shopping settlement and recharge
============================================

1. File read and write operations
2. Looping through the list
3. Shaping and converting string data types to each other

============================================
#!/usr/src/python
#-*-Coding:utf-8-*-

Import re
# Commodity Initialization
v_goods_list=[
{' name ': ' Computer ', ' price ': ' 1000 '},
{' name ': ' Mouse ', ' price ': ' 50 '},
{' name ': ' Keyboard ', ' price ': ' 100 '},
{' name ': ' Smelly socks ', ' price ': ' 2 '},
]

# list Items
For item in V_goods_list:
Print (item)
#print (V_goods_list)

# Show Balance
F1=open (' balance ', ' r ')
Balance=f1.read ()
F1.close ()

# Select Goods
V_aws=1
V_total=0
While V_aws==1:
If v_aws!=1:
Break
V_goods=input (' Select product number: ')
V_goods=int (V_goods)
V_num=input (' Quantity of Goods: ')
If v_goods==3:
Print (' Emma bought ' +v_num+ ' double ' +v_goods_list[v_goods][' name ')
V_num=int (V_num)
#加入购物车
If V_goods<len (v_goods_list):
V_total+=int (v_goods_list[v_goods][' price ') *v_num
Print (V_total)
Else
Print (' Number selection Error! ‘)
V_aws=input (' clearing? ')
v_aws = Int (v_aws)

# Billing
J=1
While J==1:
If V_total>int (balance):
Print (' Insufficient balance, please recharge ')
V_balance = input (' Recharge amount: ')
Print (v_balance)
balance = Int (v_balance) + int (balance)
Print (balance)
Elif v_total==0:
Pass
Else
Balance=balance-v_total
Print (' Buy success ')
J=0

# Balance Write back File save
F2=open (' Balance ', ' W ')
F2.write (str (balance))
F2.close ()
四、三级 linkage                                 
============================================
Mutual nesting of dictionaries and lists
For loop traversal
============================================
#!/usr/src/python
#-*-Coding:utf-8-*-

dic={
' Hebei ': {
' Shijiazhuang ': ["Luquan", "Gaocheng", "Yuanshi"],
' Handan ': ["Yongnian", "she County", "CI"]},
' Shandong ': {
' Jinan ': ["Gao Yang", "Baotu Spring", "Taian"],
' Liaocheng ': ["Qingdao", "Bozhou", "Qian County"]},
' Guizhou ': {
' Zunyi ': ["Obey the County", "Tongzi", "Michizane"],
' Bijie ': ["Nayong", "Weining", "Qianxi"]
}
}
For item in Dic.keys ():
Print (item)
V_fir_region=input (' Please select Province ')
For item in Dic[v_fir_region]:
Print (item)
V_sec_region=input (' Please select City ')
For item in Dic[v_fir_region][v_sec_region]:
Print (item)

Python basic data types and Process Control applications

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.