Python's Shopping cart writing (stay up in the code ...) )

Source: Internet
Author: User

The preparation of the shopping cart is simply too difficult for me to be a little white that is not very proficient in functions. Various pits, all kinds of helpless Ah! But finally it is written!

Not much to say, directly on the code!

#! /usr/bin/env python
#用户名 Sanjiang
#密码 sanjiang123
Import Sys
Import Collections
#定义一个计数器
A = 0
First = True
While first:
Login_user = input (' Please enter your user name: '). Strip ()
#读取用户文件user. txt, used to determine whether the input user exists
F_user = open (' User.txt ', ' r+ ')
User_list=f_user.read (). Split ()
#读取锁定文件local_file, used to determine if the user is locked in the input
Lock = open (' Local_file ', ' A + ')
Lock_file = Lock.read (). Split ()
#判断用户是否锁定
If Login_user in Lock_file:
Print ('------------------------------------------------\ n '
' Sorry, the account you entered has been locked, please log in with another account \ n ')
Continue
#判断用户是否存在
Elif Login_user not in User_list:
Print ('------------------------------------------------\ n '
' Sorry, the user you entered does not exist, please re-enter. \ n ')
Continue
While True:
Login_pwd = input (' Please enter your password: '). Strip ()
#判断密码是否正确
if login_pwd = = user_list[User_list.index (login_user) + 1]:
Print ('------------------------------------------------\ n '
' Dear%s,\n '%login_user)
First = False
Break
Else
A +=1
A = 3-a
If A ==3:
Print ('------------------------------------------------\ n '
' Sorry, you have entered more than three times, the account has been locked \ n ')
#往锁定文件中添加输入的用户
Lock.write ('%s\n '%login_user)
#关闭两个文件
Lock.close ()
F_user.close ()
First = True #定义一个变量用来跳出二层循环
Break
Print ('------------------------------------------------\ n '
' Sorry, you entered the wrong password, please re-enter. You also have%s chance \ n '%a)
#如果跳出第一层循环定义了跳出二层循环变量, then jump out of the second loop.
Print ("'
__________________________________________________
| |
| Welcome to Sanjiang Fruit Shopping mall |
|_________________________________________________|
‘‘‘)

# define a local dictionary
DIC = {}
#! Define a variable that jumps out of the loop
Flag1 = True
While Flag1:
#! request to enter a shopping budget
NUM1 = input (' Please enter your shopping budget: ')
#! Determines whether the input is empty or is a string
If Len (NUM1) ==0 or num1 = = ' 0 ' or num1.isdigit () is False:
Print (' \033[36;1m Sorry, you entered the wrong, please re-enter \033[0m ')
Continue
Else
Pass
#! Define a variable that jumps out of the loop
Flag2 = True
While Flag2:
#! Interactive query what options to do
num = input ("Sanjiang, what do you want to do?") Your budget is%s yuan
1: Buy Fruit
2: Shopping Cart
3: Modify your shopping budget
4: Boss Biography
5. Exit the program
: "%num1)
#! Defining Exit Programs
if num = = ' 5 ':
Sys.exit ()
#! Show boss Resume Information
if num = = ' 4 ':
Print ("'
Notice
Wu Sanjiang is a very handsome young man, very potential children, although now a little poor, but please do not bully young poor.

‘‘‘)
#! Return to previous loop
if num = = ' 3 ':
Flag2 = False
#! Enter the fruit purchase interface
if num = = ' 1 ':
#! define a dictionary and convert the dictionary into a list
local_dic= {' Mango ': ' 7 ', ' Peach Blossom ': ' 8 ', ' Longan ': ' 9 ', ' Apple ': ' 9 ', ' Banana ': ' 5 ', ' Grape ': ' 8 ', ' Blueberry ': ' 7 ', ' Pineapple ': ' 7 ', ' Grapefruit ': ' 3 ', ' Tangerine ': 6 ',
}
List1 =list (Local_dic)
List2 = List (Local_dic.values ())
Print (' Ref. \t\t ', ' kind \t\t ', ' Unit price \t\t ')
Flag3 = True
While FLAG3:
#! List of products showing fruit
For I in range (len (list1)):
Print (' num.%s \t\t%s \t\t%s yuan/catty '% (I,list1[i],list2[i]))
num2 = input (' \033[36;1m Please enter the serial number of the product you want to buy (go to the previous level, press Q): ')
#! Define exit this loop
if num2 = = ' Q ':
Break
#! Determines whether a string is entered
If Num2.isdigit () is False:
Print (' \033[36;1m you entered the wrong, please re-enter: \033[0m ')
Continue
If Len (num2)!=0 and int (num2) <len (list1):
#! Interactive display of fruit purchase process
num3 = input (' The product you want to buy is%s, the unit price is%s yuan/catty, OK to buy (y/n?): '% (List1[int (num2)],list2[int (num2)]))
if num3 = = ' n ':
Continue
elif num3 = = ' Y ':
NUM4 = input (' \033[36;1m Please enter the number of items you want to buy: \033[0m ')
If Num4.isdigit () is False:
Print (' \033[36;1m Sorry, you entered the wrong, please re-enter. \033[0m ')
Continue
NUM5 = input (' The product you want to buy is%s, the unit price is%s yuan/catty, the quantity is%s, the total price is%s, OK to add to the shopping cart (y/n?) : '% (List1[int (num2)],list2[int (num2)],int (NUM4), int (NUM4) *int (List2[int (num2)])))
if num5 = = ' Y ':
Dic_list = List (DIC)
Dic_list2 = List (Dic.values ())
#! If the purchased item is not in the shopping cart, then the purchase quantity is the specified quantity
If List1[int (num2)] not in dic_list:
Dic[list1[int (num2)]]=int (NUM4)
Break
#! If the item purchased is in the shopping cart, then the quantity purchased is the quantity of the previous item plus the quantity of the item.
Else
Dic[list1[int (num2)]]=int (NUM4) +dic_list2[int (Dic_list.index (List1[int (NUM2)))]

Elif num5== ' n ':
Continue
Else
Print (' \033[36;1m you entered the wrong, please re-enter: \033[0m ')
Continue
Else
Print (' \033[36;1m you entered the wrong, please re-enter: \033[0m ')
Continue
Else
Print (' \033[36;1m you entered the wrong, please re-enter: \033[0m ')
#! Go to Cart
if num = = ' 2 ':
Print ("' Shopping cart
Item Unit Price Quantity Total price \ t "")
#! Repeat the definition of a dictionary, and a list that is used to invoke
local_dic= {' Mango ': ' 7 ', ' Peach Blossom ': ' 8 ', ' Longan ': ' 9 ', ' Apple ': ' 9 ', ' Banana ': ' 5 ', ' Grape ': ' 8 ', ' Blueberry ': ' 7 ', ' Pineapple ': ' 7 ', ' Grapefruit ': ' 3 ', ' Tangerine ': 6 ',
}
Dic_list1 = List (DIC)
Dic_list2 = List (Dic.values ())
#! Show the list of items in the shopping cart
For I in range (len (dic_list1)):
Print ("'
%s%s%s%s%s\t "'% (I,dic_list1[i],local_dic[dic_list1[i]],dic_list2[i],int (dic_list2[i)) *int ( Local_dic[dic_list1[i]))
#! The total amount of all goods
A = 0
For I in range (len (dic_list1)):
A = a + int (dic_list2[i]) *int (Local_dic[dic_list1[i])
b = A
Flag4 = True
While Flag4:
NUM6 = input (' Settlement: T | exit: Q | Delete Order: D | Modify order: z\n Please enter your operation instruction: ')
if num6 = = ' Q ':
Flag4 =false
elif NUM6 = = ' t ':
num7= input (' You have purchased the total amount of fruit is%s, are you sure you want to settle it? (y/n) '%b,
if num7 = = ' Y ':
#! If the budget is less than the total amount of the settlement, then prompt to buy again
if int (NUM1) <int (b):
Print (' Sorry, it's out of your budget, please buy again. ‘)
Flag4 = False
#! If the budget is greater than the total amount of the settlement, you will be prompted to how much money is available for the budget
elif Int (NUM1) >int (b):
c = Int (NUM1)-int (b)
#! Checkout can continue shopping, you can also choose to leave
NUM8 = input ('%s ' from your budget, you can buy some fruit, do you want to leave or continue shopping? Q: Departure, shopping: G '%c)
if num8 = = ' Q ':
Flag4=false
elif NUM8 = = ' G ':
Continue
Else
Print (' Sorry. You entered the wrong ')
elif NUM6 = = ' d ':
Print (' Not fine, temporarily does not support this feature, ')
elif Num6 = = ' Z ':
Print (' Not fine, temporarily does not support this feature, ')
Else
Print (' Sorry. You entered the wrong ')
Continue
Effect display.



Python's Shopping cart writing (stay up in the code ...) )

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.