一、练习题:
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. android 圆形按钮

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools=&q ...

  2. Linux常用C函数-接口处理篇(网络通信函数)

    接口处理篇accept,bind,connect,endprotoent,endservent,getsockopt,htonl,htons,inet_addr,inet_aton,inet_ntoa ...

  3. SAP 锁对象 基本概念与基本操作 SE11

      一.SAP为什么要设置锁:     1,保持数据的一致性     假设几个用户要訪问相同的资源,须要找到一种同步訪问的方法去保持数据的一致性.比方说,在航班预订系统中,须要检查还有没有空座位,当检 ...

  4. 查询mysql字段名和字段注释

    select COLUMN_NAME,column_comment from INFORMATION_SCHEMA.Columns where table_name='表名' and table_sc ...

  5. 【dotnet跨平台】&quot;dotnet restore&quot;和&quot;dotnet run&quot;都做了些什么?

    [dotnet跨平台]"dotnet restore"和"dotnet run"都做了些什么? 前言: 关于dotnet跨平台的相关内容.能够參考:跨平台.NE ...

  6. 基于bootstrap_信息采集

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. Canvas坐标轴中的Y轴距离是X轴的两倍

    如题,相信很多人在初次玩canvas的时候会出现这样的情况,跟着教程走的情况下,诶 怎么画出来的东西,不怎么对劲啊,,,ԾㅂԾ,,!!!!!先上代码 <!DOCTYPE html> < ...

  8. 利用PHP判断iPhone、iPad、Android、PC设备

    首页那张大图确实是一个比较头疼的问题 在PC上显示是没问题的,可是到手机上就会超出页面一大截,如果做自适应,图片会被强制压缩 无奈只能用wp_is_mobile()函数在手机上隐藏了这张图,可是这函数 ...

  9. npm ERR! code EINTEGRITY npm ERR! sha1- 报错解决办法

    npm ERR! code EINTEGRITY npm ERR! sha1- 报错日志 npm ERR! code EINTEGRITY npm ERR! sha1-OGchPo3Xm/Ho8jAM ...

  10. XJTUOJ wmq的A×B Problem FFT/NTT

    wmq的A×B Problem 发布时间: 2017年4月9日 17:06   最后更新: 2017年4月9日 17:07   时间限制: 3000ms   内存限制: 512M 描述 这是一个非常简 ...