一.简述定义函数的三种方式

1.空函数:用于占位

2.有参函数:有参数的函数

3.无参函数:没有参数的函数

二.简述函数的返回值

1.如果函数没有返回值,默认返回None

2.函数可以通过return返回出返回值

3.return可以终止函数

4.return可以返回多个值,以列表形式储存

5.内置方法

三.简述函数的参数

1.形参:一般具有描述的意义,毫无作用(定义的时候写在括号里的)

2.实参:具有实际的意义,具体的一个值(传给形参)

位置形参: 从左到右一个一个写过去,就叫做位置形参

位置实参:从左到右一个一个写过,就叫做位置实参,(有多少个位置形参,就必须有多少个位置实参,从左到右依次传值)

四.编写注册函数

def register():
username_inp = input("请输入你的用户名:")
if ':' in username_inp:
print('输入错误,我必须选中一个目标')
pswd_inp = input("请输入你的密码:")
with open('user_info.txt', 'a', encoding='utf8') as fa:
fa.write(f'{username_inp}:{pswd_inp}')
print('保存成功')

五.编写登录函数

def login():
with open('user_info.txt', 'r', encoding='utf8') as fr:
data=fr.read()
data_split=data.split(':')
username = data_split[0]
pswd = data_split[1]
username_inp=input("请输入账号")
pswd_inp=input('请输入密码')
if username == username_inp and pswd == pswd_inp:
print("wdnmd")
else:
print("wtf")

六.可仿造 https://www.cnblogs.com/nickchen121/p/11070005.html 编写购物车系统

import os #join os
product_list=[ #make a product list
['watermelon',80],
['starbucks',30],
['dogfoods',200],
['human',5],
['nitendoswitch',2400],
['shoppingcart',10],
] shopping_cart={} #define a shoppingcart dictionary
current_userinfo={} #define current_userinfo dictionary db_file=r'db.txt' while True: #永久循环
print('''
press 1 to login
press 2 to register
press 3 to shopping
''')
choice=input('>>:').strip()
if choice=="1":
#1.login
tag=True
count=0
while tag:
if count==3:
print("you've tried too much please logout")
break #if you made mistakes for 3times you i'll be kicked out
uname=input('username:').strip()
pswd=input('password:').strip()#delete the blank with open(db_file,'r',encoding='utf-8')as f:
for line in f: #reading the file in db
line = line.strip('\n')
user_info=line.split(',') uname_of_db=user_info[0]
pswd_of_db=user_info[1]
balance_of_db=int(user_info[2]) if uname==uname_of_db and pswd==pswd_of_db:
print('login succeed') #登陆成功则经用户名和余额添加到列表
current_userinfo=[uname_of_db,balance_of_db]
print('userinfo is:',current_userinfo)
tag=False
break
else:
print('usename or password fault!')
count=+1 elif choice=='2':
uname=input('please enter username:').strip()
while True:
pswd1=input("please enter your password:").strip()
pswd2=input("please confirm your password:").strip()
if pswd2==pswd1:
break
else:
print('than was different passwords,please enter again!!!') balance=input('please enter the money you would like to recharge:').strip()
with open(db_file,"a",encoding='utf-8')as f:
f.write('%s,%s,%s\n' % (uname, pswd1, balance)) elif choice=='3':
if len(current_userinfo)==0:
print('please login first')
else:
uname_of_db=current_userinfo[0]
balance_of_db=current_userinfo[1] print('尊敬的用户[%s] 您的余额为[%s],祝您购物愉快' % (uname_of_db, balance_of_db)) tag=True
while tag:
for index,product in enumerate(product_list):
print(index,product)
choice=input('enter the number of goods,enter q to quit>>:').strip()
if choice.isdigit():
choice=int(choice)
if choice<0 or choice>=len(product_list):continue pname=product_list[choice][0]
pprice=product_list[choice][1]
if balance_of_db>pprice:
if pname in shopping_cart:#if you've already bought
shopping_cart[pname]['count']+=1#add one more same goods
else:
shopping_cart[pname]={
'pprice':pprice,
'count':1
} balance_of_db-=pprice#reduce your money
current_userinfo[1]=balance_of_db
print(
"Added product " + pname +
" into shopping cart,[42;1myour current balance "
+ str(balance_of_db)) else:
print("you can't afford it,you son of bitch,product price is {price},you need {lack_price}more".format(price=pprice,lack_price=(pprice-balance_of_db)))
print(shopping_cart)
elif choice=='q':
print("""
--------------------------------已购买商品列表---------------------------------
id 商品 数量 单价 总价
""")
total_cost = 0
for i, key in enumerate(shopping_cart):
print('%22s%18s%18s%18s%18s' %
(i, key, shopping_cart[key]['count'],
shopping_cart[key]['pprice'],
shopping_cart[key]['pprice'] *
shopping_cart[key]['count']))
total_cost += shopping_cart[key][
'pprice'] * shopping_cart[key]['count']#这一段看不懂
print("""
您的总花费为: %s
您的余额为: %s
---------------------------------end---------------------------------
""" % (total_cost, balance_of_db))
while tag:
inp=input('confirm(yes/no)>>:').strip()
if inp not in ['Y', 'N', 'y', 'n', 'yes', 'no']:
continue
if inp in ['Y', 'y', 'yes']:
src_file=db_file
dst_file=r'%s.swap' % db_file
with open(src_file,'r',encoding='utf-8')as read_f,\
open(dst_file,'w',encoding='utf8')as write_f:
for line in read_f:
if line.startswith(uname_of_db):
l=line.strip('\n').split(',')
l[-1]=str(balance_of_db)
line=','.join(l)+'\n' write_f.write(line)
os.remove(src_file)
os.rename(dst_file,src_file)
print('shopping succeed,please waiting for delivery') shopping_cart={}
current_userinfo=[]
tag=False
else:
print('illegal input')
elif choice =='q':
break
else:
print('illegal operate')

assignment of day nine的更多相关文章

  1. Atitit GRASP(General Responsibility Assignment Software Patterns),中文名称为“通用职责分配软件模式”

    Atitit GRASP(General Responsibility Assignment Software Patterns),中文名称为"通用职责分配软件模式" 1. GRA ...

  2. user initialization list vs constructor assignment

    [本文连接] http://www.cnblogs.com/hellogiser/p/user_initialization_list.html [分析] 初始化列表和构造函数内的赋值语句有何区别? ...

  3. Swift 提示:Initialization of variable was never used consider replacing with assignment to _ or removing it

    Swift 提示:Initialization of variable was never used consider replacing with assignment to _ or removi ...

  4. 代写assignment

    集英服务社,强于形,慧于心 集英服务社,是一家致力于优质学业设计的服务机构,为大家提供优质原创的学业解决方案.多年来,为海内外学子提供了多份原创优质的学业设计解决方案. 集英服务社,代写essay/a ...

  5. [Top-Down Approach] Assignment 1: WebServer [Python]

    Today I complete Socket Programming Assignment 1 Web Server Here is the code: #!/usr/bin/python2.7 # ...

  6. default constructor,copy constructor,copy assignment

     C++ Code  12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 ...

  7. Programming Assignment 5: Kd-Trees

    用2d-tree数据结构实现在2维矩形区域内的高效的range search 和 nearest neighbor search.2d-tree有许多的应用,在天体分类.计算机动画.神经网络加速.数据 ...

  8. Programming Assignment 4: 8 Puzzle

    The Problem. 求解8数码问题.用最少的移动次数能使8数码还原. Best-first search.使用A*算法来解决,我们定义一个Seach Node,它是当前搜索局面的一种状态,记录了 ...

  9. Programming Assignment 2: Randomized Queues and Deques

    实现一个泛型的双端队列和随机化队列,用数组和链表的方式实现基本数据结构,主要介绍了泛型和迭代器. Dequeue. 实现一个双端队列,它是栈和队列的升级版,支持首尾两端的插入和删除.Deque的API ...

  10. 逆转序列的递归/尾递归(+destructuring assignment)实现(JavaScript + ES6)

    这里是用 JavaScript 做的逆转序列(数组/字符串)的递归/尾递归实现.另外还尝鲜用了一下 ES6 的destructuring assignment + spread operator 做了 ...

随机推荐

  1. 微软引入了两种新的网络过滤系统,WFP和NDISfilter

    Windows 8是微软公司推出的最新的客户端OS,内部名称Windows NT 80.相对于Windows NT 5.x,其网络结构变化非常大,原有的TDI,NDIS系统挂接方法不再适用.在Wind ...

  2. 21. Jmeter对数据库进行压力测试

    测试工作中有时候会对数据库进行压力测试,jmeter实现这个需求较为简单,在这里简单介绍下.可以参考我之前写的 15. Jmeter-配置元件二 步骤: 1.选中测试计划,添加mysql-connec ...

  3. 剑指offer——04二维数组中的查找

    题目: 数组中唯一只出现一次的数字.在一个数组中除一个数字只出现一次之外,其他数字都出现了三次.请找出那个只出现一次的数字. 题解: 如果一个数字出现三次,那么它的二进制表示的每一位(0或者1)也出现 ...

  4. window_mysql踩坑

    https://blog.csdn.net/qq_37350706/article/details/81707862 先去官网下载点击的MySQL的下载 下载完成后解压 解压完是这个样子 配置系统环境 ...

  5. C语言——二维数组

    目录 二维数组 一.二维数组的定义 二.二维数组的初始化 三.通过赋初值定义二维数组的大小 四.二维数组与指针 二维数组 一.二维数组的定义 类型名 数组名[ 常量表达式1 ][ 常量表达式2 ] i ...

  6. MySQL数据库(五)—— 用户管理、pymysql模块

    用户权限管理.pymysql模块 一.用户管理(权限管理) 在MySQL中自带的mysql数据库中有4个表用于用户管理的 # 优先级从高到低 user > db > tables_priv ...

  7. 牛客OI月赛12-提高组题解

    牛客OI月赛12-提高组 当天晚上被\(loli\)要求去打了某高端oj部分原创的模拟赛,第二天看了牛客的题觉得非常清真,于是就去写了 不难发现现场写出\(260\text{pts}\)并不需要动脑子 ...

  8. sanic之websocket路由

    在某些时候,需要建立websocket路由,来建立长链接,来实时传输数据,就比如一些聊天应用,就有实时音视频,需要实时传出状态 在sanic框架中支持两种websocket路由方式,有一种是再app中 ...

  9. 案例-开门效果CSS3

    <style> .door { width: 288px; height: 153px; border: 2px solid #333; margin: 150px auto; backg ...

  10. etc/profile /etc/bashrc ~/.bash_profile ~/.bashrc等配置文件区别

    什么是交互式shell和非交互式shell,什么是login shell 和non-login shell. 交互式模式:就是shell等待你的输入,并且执行你提交的命令.这种模式被称作交互式是因为s ...