9.2作业
作者:互联网
1.简述定义函数的三种方式
答:1空函数,2无参函数,3含参函数
2.简述函数的返回值
答:函数的返回值为当此函数结束时输出给控制台的数据,可用return 语句自定义,默认值为None
3.简述函数的参数
答:函数的参数包括形参和实参,形参为定义函数时的参数,包括位置参数和关键字参数。定义时所定义的位置参数和关键字参数可作为函数默认值。定义过程中应避免关键字参数放在位置参数之前。实参为调用函数时实际给出的参数,如原函数仅有位置参数,则实参个数需与位置参数个数相同,关键字参数赋值时会将原形参覆盖。
4.5编写注册函数,登录函数
答:
def register_test():
print('请注册')
usersname_ipt = input('请输入用户名:')
print('用户名为:',usersname_ipt)
userspwd_ipt = input('请输入密码:')
print('密码为:',userspwd_ipt)
with open('user_info.txt','a',encoding='utf8') as fa:
fa.write(f'{usersname_ipt}:{userspwd_ipt}')
print('保存成功')
def login():
print('登录')
with open('user_info','r',encoding='utf8') as fr:
data = fr.read()
data_split = data.split(':')
usersname,psd = data_split[0],data_split[1]
usersname_ipt = input('请输入用户名:')
userspwd_ipt = input('请输入密码:')
if usersname = usersname_ipt and psd = userspsd_ipt:
print('登录成功')
else:
print('登录失败')
周六到的,with下面抄的,自己不会写
6.购物车系统
答:抄了一遍
x = int(input('x:'))
y= int(input('y:'))
def mix111(x,y):
if x> y :
print(x)
else:
print(y)
def compare():
x = 10
y = 20
if x>y:
print(x)
else:
print(y)
return 1
compare()
print(compare)
print(compare())
def register_test():
print('请注册')
usersname_ipt = input('请输入用户名:')
print('用户名为:',usersname_ipt)
userspwd_ipt = input('请输入密码:')
print('密码为:',userspwd_ipt)
with open('user_info.txt','a',encoding='utf8') as fa:
fa.write(f'{usersname_ipt}:{userspwd_ipt}')
print('保存成功')
def login():
print('登录')
with open('user_info','r',encoding='utf8') as fr:
data = fr.read()
data_split = data.split(':')
usersname,psd = data_split[0],data_split[1]
usersname_ipt = input('请输入用户名:')
userspwd_ipt = input('请输入密码:')
if usersname = usersname_ipt and psd = userspsd_ipt:
print('登录成功')
else:
print('登录失败')
import os
product_list=[
['iphone7',5800],
['coffee',30],
['python book',99],
['bike',299],
['vivo x9',2499],]''
shopping_cart={}
current_userinfo = []
db_file = r'db.txy' #r是模糊搜索
while True:
print('''
登录
注册
购物
'''
)
choice = input('>>:').strip()
if choice == '1':
#1,登录
tag = True
count = 0
while tag:
if count == 3:
print('尝试次数过多,退出')
break
uname = input('用户名那个:').strip()
pwd = input('密码:').strip()
with open(db_file,'r',encoding='utf-8') as f :
for line in f:
line = line.strip('\n')
user_info = line.split(',')
uname_of_db = user_info[0]
pwd_of_db = user_info[1]
balance_of_db = int(user_info[2])
if uname == uname_of_db and pwd == pwd ==pwd_of_db:
print('登录成功')
#登录成功则将用户名和愈合添加到列表
current_userinfo = [uname_of_db,balance_of_db]
print('用户名信息为:',current_userinfo)
tag = False
break
else:
print('用户名密码错误')
count += 1
elif choice == '2':
uname = input('请输入用户名:').strip()
while True:
pwd1 = input('请输入密码:').strip()
pwd2 = input('请再次确认密码:').strip()
if pwd2 == pwd1:
break
else:
print('两次输入密码不一致,请重新输入:')
balance = input('请输入充值金额:').strip()
with open(db_file,'a',encoding='utf-8') as f:
f.write('%s,%s,%s\n' %(uname,pwd1,balance))
elif choice == '3':
if len(current_userinfo) == 0:
print('请先登录。。。')
else:
#登录成功后,开始购物
uname_of_db = current_userinfo[0]
balance_of_db = current_userinfo[1]
print('尊敬的用户[%s]您的余额为[%s],祝您购物愉快'%(uname_of_db,balance_of_db))
tag = True
whlie tag:
for index,product in enumerate(product_list):
print(index,product)
choice = input('输入商品编号购物,输入q推出>>').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 pmane in shopping_cart:#原来已经购买过
shopping_cart[pname]['count'] += 1
else:
shopping_cart[pname] = {
'pprice':pprice,
'count':1
}
balance_of_db -= pprice#扣钱
current_userinfo[1] = balance_of_db#更新 用户余额
print(
'Added product' + pname + 'into shopping cart,[42;1myour current balance'
+str(balance_of_db))
else:
print('买不起,穷逼!产品价格是{price},你还差{lack_price}'.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,ke,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('确认购买(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='utf-8') as write_f:
for line in read_f:
if line.startswith(uname_of_db):
l = line.strip('\n').strip(',')
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_cart = {}
current_userinfo = {}
tag = False
else:
print('输入非法 ')
elif choice == 'q':
break
else:
print('非法操作')
标签:usersname,作业,db,cart,ipt,print,input,9.2 来源: https://www.cnblogs.com/agsol/p/11448291.html