一、练习题:
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基础作业的更多相关文章

  1. python基础作业1

    目录 附加练习题(提示:一步步拆解) 1.想办法打印出jason 2.想办法打印出大宝贝 3.想办法打印出run 4.获取用户输入并打印成下列格式 5 根据用户输入内容打印其权限 6 编写用户登录程序 ...

  2. python基础作业2

    目录 编写一个用户认证装饰器 利用有参装饰器编写多种用户登录校验策略 利用递归函数依次打印列表中每一个数据值 获取用户权限并校验用户登录 编写一个用户认证装饰器 """ ...

  3. 03 python基础作业(一)

    1.将['alex','eric',’rain’]用下划线拼接成字符串.(['alex','eric',123]呢?) li=['alex','eric','rain'] v='_'.join(li) ...

  4. python基础一之课后作业:编写登录接口

    1 # Author : Mamba 2 3 #python基础一之课后作业:编写登录接口 4 5 # 输入用户名密码 6 # 认证成功后显示欢迎信息 7 # 用户名3次输入错误后,退出程序 8 # ...

  5. python基础 实战作业 ---Excel基本读写与数据处理

    代码地址如下:http://www.demodashi.com/demo/11650.html 看完本篇需要: 10min 作业练习需要: 0.5h~3h(依练习者对python熟悉程度而定) 看完本 ...

  6. python开发基础作业01:模拟登陆系统

    随老男孩学习python mark 作业要求及提示:编写登录接口 ''' 练习程序:编写登录接口 1. 输入用户名和密码 2. 认证成功后显示欢迎信息 3. 输错三次后锁定 输入三次后退出,下次同样用 ...

  7. Python之路3【第一篇】Python基础

    本节内容 Python简介 Python安装 第一个Python程序 编程语言的分类 Python简介 1.Python的由来 python的创始人为吉多·范罗苏姆(Guido van Rossum) ...

  8. Python基础s14-day1

    2016年7月23日"Python基础s14-Day1" Python是什么? Python(英国发音:/ˈpaɪθən/ 美国发音:/ˈpaɪθɑːn/),是一种面向对象.直译式 ...

  9. python基础——使用dict和set

    python基础——使用dict和set dict Python内置了字典:dict的支持,dict全称dictionary,在其它语言中也称为map(映射),使用键-值(key-value)存储,具 ...

  10. Python之路【第二篇】:Python基础(一)

    一.作用域 对于变量的作用域,执行声明并在内存中存在,该变量就可以在下面的代码中使用. 1 2 3 if 1==1:     name = 'wupeiqi' print  name 下面的结论对吗? ...

随机推荐

  1. CSS3中transition-duration參数对hover前后两种过渡时间的影响

    transition-duration这个參数是设置过渡时间的,将transition-duration放在哪个类中.那么在这个类被启用时就会依照transition-duration设定的时间来过渡 ...

  2. 课程的正确步调——Leo鉴书74

    <Leo鉴书(第1辑)>已登陆百度阅读.今后还将不断更新,免费下载地址:http://t.cn/RvawZEx 本人第一次站上讲台是1999年,那会儿从中关村回到天津,在一个给成人做计算机 ...

  3. 解决查询access数据库含日文出现“内存溢出”问题

    ACCESS有个BUG,那就是在使用 like 搜索时如果遇到日文就会出现“内存溢出”的问题,提示“80040e14/内存溢出”. 会出问题的SQL: where title like '%" ...

  4. 【转载】TCP的三次握手(建立连接)和四次挥手(关闭连接)

    建立连接: 理解:窗口和滑动窗口TCP的流量控制 TCP使用窗口机制进行流量控制 什么是窗口? 连接建立时,各端分配一块缓冲区用来存储接收的数据,并将缓冲区的尺寸发送给另一端 接收方发送的确认信息中包 ...

  5. 释放SQL Server占用的内存 .Net 读取xml UrlReWriter 在web.config中简单的配置

    释放SQL Server占用的内存   由于Sql Server对于系统内存的管理策略是有多少占多少,除非系统内存不够用了(大约到剩余内存为4M左右),Sql Server才会释放一点点内存.所以很多 ...

  6. mac系统下为emacs设置中文字体,解决乱码问题

    近期换了个系统,如今用mac系统. 当打开emacs后,中文支持的不是非常好.有的地方能显示.在.el文件的凝视里显示为口口口口口口口口这种框.例如以下图所看到的 找了半天.是由于中文字体的问题.仅仅 ...

  7. 关于cocos2d-x 3.0的点击交互处理

    转自:http://blog.csdn.net/fansongy/article/details/12716671 1.概述     游戏也好,程序也好,仅仅有能与用户交互才有意义.手机上的交互大致能 ...

  8. (转)gcc学习笔记

    1.gcc -Wall hello.c -o hello //编译源文件,显示警告信息 2../a.out   //运行程序 3.gcc -Wall calc.c /usr/lib/libm.a -o ...

  9. python使用cx_oracle连接oracle数据库

    http://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html---下载instantclient-basic-linux.x ...

  10. (org.openqa.selenium.WebDriverException: Unable to launch the app: Error: Trying to start logcat capture but it's already started! )错误解决办法

    新增: capabilities.setCapability("autoLaunch",false); 将setup中的: driver = new AndroidDriver(n ...