学习课本作业 登录密码和菜单购物

登录密码

 1 # -*- coding: utf-8 -*-
 2 #__author__ = 'Administrator'
 3 
 4 _username = 'abc'
 5 _password = int('123')
 6 
 7 count = 0
 8 while count < 3:
 9     username = input ('用户姓名 : ')
10     password = int(input('密码 : '))
11     if _username == username and _password == password:
12         print('Wellcome user {name} ! '.format(name = username))
13         break
14     elif  _username == username and _password != password:
15         print('wrong password !')
16     else :
17         print('wrong username ! ')
18     count +=1
19     if count==3:
20         receive = input('Do you want to try ? yes or no ')
21         if receive!='no':
22             count = 0
23 else :
24     print("you have tried too man times ...")
View Code

餐单

 1 # -*- coding: utf-8 -*-
 2 #__author__ = 'Administrator'
 3 
 4 product_list = [('Iphone',5800),('Mac Pro',9800),('Bike',800),('Watch',10600),('Coffee',31),('Python book',120)]
 5 shopping_list = []
 6 salary = input('Input your salary :')
 7 if salary.isdigit():  #如果他为整型为真
 8     salary = int(salary)
 9     while True:
10         for item in product_list :
11             print(product_list.index(item),item) #index 下标
12         user_choice = input("what do you want to buy ?")
13         if user_choice.isdigit():          #我判断下是否选择正确数字
14             user_choice = int(user_choice)
15             if user_choice < len(product_list) and user_choice >=0 :  #比较是否正确范围
16                 p_item = product_list[user_choice]
17                 if p_item[1] < salary :     #买得起
18                     shopping_list.append(p_item)  #存到购物车里
19                     salary -= p_item[1]
20                     print('你买了[%s],你的余额还剩\033[32;1m[%s]\033[0m'% (p_item[0],salary))
21                     print('你还想要买些什么?退出输入q!')
22                 else :
23                     print('\033[31;1m你的余额只剩[%s]啦,还买个毛线\033[0m' % salary)
24             else :
25                 print("product code [%s] is not exist!" % user_choice)
26         elif user_choice == 'q':
27             print('------shopping list-------')
28             for p in shopping_list:
29                 print(p)
30             print ('购物完成你还剩\033[31;1m[%s]\033[0m'% salary)
31             exit ()
32         else :
33             print('invalid choise !')
34 else :
35     print ('invalid salary !')
View Code

 

posted @ 2020-02-09 20:17  穆帅虎  阅读(99)  评论(0)    收藏  举报