Python3 from zero single row 3_ function (Buy product applet)

Source: Internet
Author: User

The topics are as follows:
The contents of the commodity document Products.txt are as follows:
{' Mac ': 6500, ' quilt ': 100.0, ' Mobile ': 1.0, ' BMW ': 100}
The contents of the user file User.txt are as follows:
{' Dalei ': {' money ': "1003.0, ' role ': 2, ' Pass ': ' 123456 ', ' products ': [' mac ']}, ' xg ': {' money ': +, ' role ': 1, ' Pass ': ' 12 3456 ', ' products ': [' Mac ']}}
The administrator can manipulate:
1. Recharge the user, enter the user name, modify the money,100000
2. Add Items
3.logout
Normal user can operate:
1. See Product information
2. Buy something, A. Things plus Products,money to modify
3. View your products and balances
4. Exit

Requirements Analysis:
1. Product, user file initialization data
2. Log in to read user files
3. Login to get the role, give the corresponding menu dictionary to get the user role, define the dictionary for the user to select the appropriate action
4. Administrator
1. Recharge the user, enter the user name, modify the money,100000
2. Add Items
3.logout
5. User
1. See Product information
2. Buy something, A. Things plus Products,money to modify
3. View your products and balances
4. Exit

  The code is as follows:

#define read-write filesdefOp_file (filename,content=None):ifContent:with open (filename,'W', encoding='Utf-8') as F:f.write (str (content))Else: With open (filename,'R', encoding='Utf-8') as F:res=eval (F.read ())returnRes#define input legitimacy for the checksumdefCheckmoney (Money):ifmoney.isdigit ():returnTrueelifMoney.count ('.') ==1:        ifMoney.split ('.') [0].isdigit () andMoney.split ('.') [1].isdigit ():returnTruereturnFalse#Defining Loginsdeflogin (): Username=input ('User name:') Password=input ('Password:') Users=op_file ('User.txt')    ifUsernameinchUsers andusers[username]['Pass']==Password:Print('Welcome to "%s" Login'%username)returnusername,users[username]['role']    Else:        Print('Login failed! ')#Administrator#1. Recharge the user, enter the user name, modify the money,100000defcharge (): Username= Input ('User name:'). Strip () money= Input ('Amount:'). Strip () Users= Op_file ('User.txt')    ifUsernameinchUsers andCheckmoney (Money): users[username][' Money']+=Float (Money) op_file ('User.txt', content=users)#2. Add ItemsdefAdd (): Sp_name= Input ('Product Name:'). Strip () Sp_money= Input ('Amount:'). Strip () SP= Op_file ('Products.txt')    ifCheckmoney (Sp_money): Sp[sp_name]=float (Sp_money) op_file ('Products.txt', content=sp)#3.logoutdeflogout (): Exit ('Exit Program')#5. User#1. See Product informationdefShow (): SP= Op_file ('Products.txt')     forIinchSP:Print('Product:%s, Price:%s'%(I,sp[i]))#2. Buy something, A. Things plus Products,money to modifydefBuy (user): Pro=input ('Buy the goods Migncheng:') SP= Op_file ('Products.txt') Users= Op_file ('User.txt')    ifProinchSP:ifusers[user][' Money']>=Sp[pro]: users[user][' Money'] -=Sp[pro] users[user][' Products'].append (Pro) Op_file ('User.txt', content=users)Else:            Print('Please contact the administrator to recharge! ')    Else:        Print('product not on shelves! ') Buy (user)#3. View your products and balancesdefLook (user): Users= Op_file ('User.txt')    Print('your Shopping cart has:%s'%str (users[user][' Products']))    Print('your balance is:%.2f'%users[user][' Money']) Choice_admin={'1': Charge,'2': Add,'3': Logout}choice_user={'1': Show,'2': Buy,'3': Look,'4': Logout}defrun (): User,role=Login ()ifRole==1:         whileTrue:choice=input ('Please enter your choice: 1. Recharge; 2. Add a product; 3. Exit')            ifchoice=='1' orchoice=='2' orchoice=='3': Choice_admin[choice] ()Else:                Print('wrong input! ')    Else:         whileTrue:choice=input ('Please enter your choice: 1. View the goods, 2. Purchase, 3. Check the cart and balance; 4. Exit')            ifchoice=='1' orchoice=='4': Choice_user[choice] ()elifchoice=='2' orchoice=='3': Choice_user[choice] (user)Else:                Print('wrong input! ')                if __name__=='__main__': Run ()

Python3 from zero single row 3_ function (Buy product applet)

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.