017--python基础作业
一、练习题:
1、使用while循环输入 1 2 3 ... 8 9 10
2、求1-100的所有数的和
3、输出 1-100 内的所有奇数
4、输出 1-100 内的所有偶数
5、求1-2+3-4 ... 99的所有数的和
# 练习题:
# 1、使用while循环输入 1 2 3 ... 8 9 10
count = 0
while count < 10:
number = input('>>:').strip()
print(number)
count+=1 # 2、求1-100的所有数的和
print(sum(range(1,101))) # 3、输出 1-100 内的所有奇数
for i in range(1,100):
if i%2!=0:
print(i) # 4、输出 1-100 内的所有偶数
for i in range(1,100):
if i%2==0:
print(i) # 5、求1-2+3-4 ... 99的所有数的和
s=0
for n in range(1,100):
if n%2==0:
n=-n
s+=n
print (s)
练习题 Code
二、模拟登陆
1. 用户输入帐号密码进行登陆
2. 用户信息保存在文件内
3. 用户密码输入错误三次后锁定用户"
# 模拟登陆
#1. 用户输入帐号密码进行登陆
#2. 用户信息保存在文件内
#3. 用户密码输入错误三次后锁定用户"
import sys
count = 0
flag_break =False
while count<3:
user_name = input('用户名:').strip()
with open('lock_file',encoding='utf-8') as f_lock:
for line in f_lock:
if line.strip() == user_name:
sys.exit('%s 已经被锁定' % user_name)
password =input('密码:').strip()
with open('user_file',encoding='utf-8') as f_user:
for line in f_user:
username,passwd = line.strip().split()
if user_name == username and password == passwd:
print('登录成功')
flag_break = True
break
if flag_break == False:
if count == 2:
count += 1
else:
print('用户名或者密码错误,请重试,还有 %s次机会' % (2 - count))
count += 1
else:
break
else:
print('多次登录错误,此账户已经被锁定')
with open('lock_file', 'a+') as f_lock:
f_lock.write('\n' +user_name)
模拟登陆 Code
三、三级菜单:
1. 运行程序输出第一级菜单
2. 选择一级菜单某项,输出二级菜单,同理输出三级菜单
3. 返回上一级菜单和顶部菜单
4. 菜单数据保存在文件中"
{
'北京':{
'海淀':{
'五道口':{
'soho':{},
'网易':{},
'google':{}
},
'中关村':{
'爱奇艺':{},
'汽车之家':{},
'youku':{},
},
'上地':{
'百度':{},
},
},
'昌平':{
'沙河':{
'老男孩':{},
'北航':{},
},
'天通苑':{},
'回龙观':{},
},
'朝阳':{},
'东城':{},
},
'上海':{
'闵行':{
"人民广场":{
'炸鸡店':{}
}
},
'闸北':{
'火车战':{
'携程':{}
}
},
'浦东':{},
},
'山东':{},
}
菜单文件 Code
current_layer = {}
last_layers = []
with open('menu',encoding='utf-8') as menu_file:
f = menu_file.read()
file_str = str(f) # 将文本信息转成字符串格式
current_layer = eval(file_str) # 字符串转成字典格式
while True:
for key in current_layer:
print(key)
choice = input('>>:').strip()
if len(choice) == 0:continue
if choice in current_layer: #进入下一层
last_layers.append(current_layer) #将上一层加入列表,待后期返回调用
current_layer = current_layer[choice] #将下一层更新为当前层
if choice == 'b': #返回上一层
if last_layers: #保证列表不为空
current_layer = last_layers[-1] #取上一层更新为当前层
last_layers.pop() #删除列表的最后一个
if choice == 'q':break #退出
if choice == 'top': #返回顶层
current_layer = last_layers[0]
三级菜单 Code
四、购物车
1. 商品信息- 数量、单价、名称
2. 用户信息- 帐号、密码、余额
3. 用户可充值
4. 购物历史信息
5. 允许用户多次购买,每次可购买多件
6. 余额不足时进行提醒
7. 用户退出时 ,输出档次购物信息
8. 用户下次登陆时可查看购物历史
9. 商品列表分级
# 购物车
#1. 商品信息- 数量、单价、名称
#2. 用户信息- 帐号、密码、余额
#3. 用户可充值
#4. 购物历史信息
#5. 允许用户多次购买,每次可购买多件
#6. 余额不足时进行提醒
#7. 用户退出时 ,输出当次购物信息
import sys
flag_break =False
old_user = True
while True:
user_name = input('用户名:').strip()
with open('old_user',encoding='utf-8') as f_old:
for line in f_old:
if line.strip() == user_name:
old_user = True #判断是老用户标志位
password =input('密码:').strip()
with open('shopping_user',encoding='utf-8') as f_user:
for line in f_user:
username,passwd,balance = line.strip().split()
if user_name == username and password == passwd:
print('登录成功,您的余额:%s'%balance)
salary = int(balance)
flag_break = True
if not flag_break:
sys.exit('登录失败')
if old_user: #执行对于老用户打印历史购物信息的工作
with open('shopping_history',encoding='utf-8') as f_history:
file_history = f_history.read()
print('您以前购买过以下商品'.center(50,'-'))
print('id 商品 数量 单价 总价')
print(file_history.strip())
print('end'.center(60,'-')) shopping_cart = {} #购物车
product_list = [
['自行车',999],
['充电宝',555],
['茅台酒',650],
['铂金笔',500],
['电饭锅',200]
]
while True:
index = 0
for product in product_list:
print(index,product)
index+=1
choice = input('请输入商品编号,或者q直接退出')
if choice=='q':
print('您已购买以下商品'.center(50,'-'))
id_count = 1
total_cost = 0
print('id 商品 数量 单价 总价')
for key in shopping_cart:
msg = ("%s\t\t%s\t\t%s\t\t%s\t\t%s"
%(id_count,
key,
shopping_cart[key][1],
shopping_cart[key][0],
shopping_cart[key][1]*shopping_cart[key][0]
)
)
print(msg)
id_count+=1
total_cost+=shopping_cart[key][1]*shopping_cart[key][0]
with open('shopping_history', 'a+', encoding='utf-8') as f_history:
f_history.write('\n' + msg)
print('您的总花费为:',total_cost)
print('您的余额为:'+str(salary))
print('end'.center(60,'-'))
with open('old_user', 'a+',encoding='utf-8') as f_old:
f_old.write('\n' + user_name)
sys.exit() number = input('请输入购买数量:')
if choice.isdigit() and number.isdigit():
choice = int(choice)
number = int(number)
if choice >= 0 and len(product_list) >= choice:
product = product_list[choice]
if product[1]*number <= salary:
if product[0] in shopping_cart:
shopping_cart[product[0]][1] += number #[price ,数量] 只需要把数量+1 ,加入购物车
else:
shopping_cart[product[0]] = [product[1],number]
salary-=product[1]*number
print('您已购买 %s个%s ,您的余额为:%s'%(number,product[0],salary))
else:
print("商品的价格为:%s,购买数量%s个,还差%s元"%(product[1],number,product[1]*number-salary))
more_money = input('你想要充值吗?y/n:').strip()
if more_money == 'n':
sys.exit()
if more_money == 'y':
recharge = int(input('充值:').strip())
salary+=recharge
else:
print('商品编号不存在,请重新输入') else:
print('编号不存在,请重新输入')
购物车 Code
017--python基础作业的更多相关文章
- python基础作业1
目录 附加练习题(提示:一步步拆解) 1.想办法打印出jason 2.想办法打印出大宝贝 3.想办法打印出run 4.获取用户输入并打印成下列格式 5 根据用户输入内容打印其权限 6 编写用户登录程序 ...
- python基础作业2
目录 编写一个用户认证装饰器 利用有参装饰器编写多种用户登录校验策略 利用递归函数依次打印列表中每一个数据值 获取用户权限并校验用户登录 编写一个用户认证装饰器 """ ...
- 03 python基础作业(一)
1.将['alex','eric',’rain’]用下划线拼接成字符串.(['alex','eric',123]呢?) li=['alex','eric','rain'] v='_'.join(li) ...
- python基础一之课后作业:编写登录接口
1 # Author : Mamba 2 3 #python基础一之课后作业:编写登录接口 4 5 # 输入用户名密码 6 # 认证成功后显示欢迎信息 7 # 用户名3次输入错误后,退出程序 8 # ...
- python基础 实战作业 ---Excel基本读写与数据处理
代码地址如下:http://www.demodashi.com/demo/11650.html 看完本篇需要: 10min 作业练习需要: 0.5h~3h(依练习者对python熟悉程度而定) 看完本 ...
- python开发基础作业01:模拟登陆系统
随老男孩学习python mark 作业要求及提示:编写登录接口 ''' 练习程序:编写登录接口 1. 输入用户名和密码 2. 认证成功后显示欢迎信息 3. 输错三次后锁定 输入三次后退出,下次同样用 ...
- Python之路3【第一篇】Python基础
本节内容 Python简介 Python安装 第一个Python程序 编程语言的分类 Python简介 1.Python的由来 python的创始人为吉多·范罗苏姆(Guido van Rossum) ...
- Python基础s14-day1
2016年7月23日"Python基础s14-Day1" Python是什么? Python(英国发音:/ˈpaɪθən/ 美国发音:/ˈpaɪθɑːn/),是一种面向对象.直译式 ...
- python基础——使用dict和set
python基础——使用dict和set dict Python内置了字典:dict的支持,dict全称dictionary,在其它语言中也称为map(映射),使用键-值(key-value)存储,具 ...
- Python之路【第二篇】:Python基础(一)
一.作用域 对于变量的作用域,执行声明并在内存中存在,该变量就可以在下面的代码中使用. 1 2 3 if 1==1: name = 'wupeiqi' print name 下面的结论对吗? ...
随机推荐
- String、StringBuffer和StringBuilder的差别
String.StringBuffer和StringBuilder的差别 1.String类是不可变类,即一旦一个String对象被创建后.包括这个对象中的字符序列是不可改变的 2.在字符串拼接的过程 ...
- zabbix之Low-level discovery(自动发现)
功能: 自动发现挂载点并自动监控 自动发现网卡并自动监控 创建自动发现挂载点并监控 功能 假如要在monitor_linux模板中创建自动发现挂载点的功能 配置-->模板-->选择模板-- ...
- HDU2897( 巴什博奕变形)
邂逅明下 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissi ...
- hdu 1710 Binary Tree Traversals 前序遍历和中序推后序
题链;http://acm.hdu.edu.cn/showproblem.php?pid=1710 Binary Tree Traversals Time Limit: 1000/1000 MS (J ...
- [原创] 在线音乐API的研究 (Part 2.1)
最近,在优化一个自己写的音乐播放器.主要目的是回顾.归纳,并希望能够写出一个属于自己的common lib.今天,主要是关于在线音乐API的一些分析结果.此次,主要分析的是歌词.专辑部分.在线搜索音乐 ...
- 用python编写的简易端口扫描器
#coding = utf-8 ''' python 3.4 通过简单的TCP端口连接来判断指定IP是否开放了指定端口. ''' import socket import optparse impor ...
- 梳理caffe代码common(八)
因为想梳理data_layer的过程.整理一半发现有几个很重要的头文件就是题目列出的这几个: 追本溯源,先从根基開始学起.这里面都是些什么鬼呢? common类 命名空间的使用:google.cv.c ...
- CString转换成char *字符串问题
buf = (LPSTR)(LPCTSTR)str; ==> buf 显示的是第一个字符 strcpy(pNumber,strNumber); ==> e ...
- lonlifeOJ1152 “玲珑杯”ACM比赛 Round #19 概率DP
E -- Expected value of the expression DESCRIPTION You are given an expression: A0O1A1O2A2⋯OnAnA0O1A1 ...
- gradle in action 笔记
原网址 https://lippiouyang.gitbooks.io/gradle-in-action-cn/content/