Commit 2eb5b88d by BellCodeEditor

save project

parent bbaedb3a
Showing with 103 additions and 33 deletions
# #第一题
# # 写入同学们的捐赠明细:'小兰:12本'、'小丽:11本'、'李文:9本'、'张伟:16本'
# # 打开、创建文件,这里的路径要换成自己哦~
# file1= open(r'C:\Users\windoins\Desktop\test.txt','w',encoding='utf-8')
# # 写入数据
# file1.write('小兰:12本')
# file1.write('小丽:11本')
# file1.write('李文:9本')
# file1.write('张伟:16本')
# # 关闭文件
# file1.close()
# #第二题
# # 补充数据的第一种写法:
# file= open(r'C:\Users\windoins\Desktop\test.txt','w',encoding='utf-8')
# file.write('小强:10本\n')
# file.write('李明:15本\n')
# file.close()
# # 补充数据的第二种写法:
# with open(r'C:\Users\windoins\Desktop\test.txt','w',encoding='utf-8') as file:
# file.write('小强:10本\n')
# file.write('李明:15本\n')
#第三题
# 完成修改数据前的遍历和条件判断,找下'小强:10本'在不在数据中,以及小兰的数据'小兰:12本'在不在
with open(r'C:\Users\CM\Desktop\book.txt','a',encoding='utf-8') as file1:
for date in file1:
if "小强:10" in date
print('小强数据在文件中')
if "小兰:10" in date
print('小兰数据在文件中')
# 第一题
# # 这是悟空为花果山小猴做臂力测试的程序代码
# name=input('你叫啥名啊?')
# power=int(input('你臂力多少啊?'))
# list_hero=['猴三',10,'猴一',21,'猴五',22,'猴队长',29,'猴七',30]
# print(list_hero)
# # 请注释掉上面的代码,并在下一行创建一个名为dict_hero的字典
# dict_hero={'猴三':10,'猴一':21,'猴五':22,'猴队长':29,'猴七':30}
# print(dict_hero)
# 第二题
# 提取猴队长的臂力值
# 修改猴七的臂力值为32
# print(dict_hero['猴队长'])
# dict_hero['猴七']=32
# print(dict_hero)
# 第三题
# 猴十的臂力值是25,请补充进字典
# dict_hero['猴十']=25
# print(dict_hero)
# 第四题
# 神奇百货中的物品已经帮你创建好啦,接下来要看你的了~
# dict= {'可口可乐':3,'旺仔牛奶':4,'农夫山泉':1,'辣条':3,'巴西烤肉':2,'果冻':4,'乐事':5,'奥利奥':10,'巧克力':6}
# j=input("你要买什么?")
# if j in dict:
# print('你的'+j+'需要'+str(dict[j])+'元')
# else:
# print('你要的东西没有,滚,下一个!')
# 第五题
# 持家小能手 (利用自动更新成绩程序的逻辑完成这个新项目)
# 如果输入的水果价钱比字典中的低,则替换掉,反之则不更新
cost= {'苹果':5.2,'山竹':12.9,'香蕉':2.4,'荔枝':15,'葡萄':9.3,'桂圆':8,'蓝莓':10,'李子':8}
k=input('请输入水果名称:')
v=input('请输入水果价钱:')
# ?补充条件判断:
if k in cost:
if cost[k]>float(v):#更新价钱
cost[k]=float(v)
print(k+'降价了,现价'+v+'元')
else:
print(k+'涨价了,我们不买')
else:
cost[k] = float(v)
print(k+'的价钱'+v+'元已上传~')
# # 请注释掉上面的代码,并在下一行创建一个名为dict_hero的字典
# dict_hero={'猴三':10,'猴一':21,'猴五':22,'猴队长':29,'猴七':30}
# print(dict_hero)
# # 提取猴队长的臂力值
# print(dict_hero['猴队长'])
# # 修改猴七的臂力值为32
# dict_hero['猴七']=32
# print(dict_hero)
# # 猴十的臂力值是25,请补充进字典
# dict_hero['猴十']=25
# print(dict_hero)
# # 神奇百货中的物品已经帮你创建好啦,接下来要看你的了~
# dict= {'可口可乐':3,'旺仔牛奶':4,'农夫山泉':1,'辣条':3,'巴西烤肉':2,'果冻':4,'乐事':5,'奥利奥':10,'巧克力':6}
# k=input('你想买什么呀?')
# if k in dict:
# print('叮咚~您的'+k+'需要支付'+str(dict[k])+'元~')
# else:
# print('神奇百货暂未有你想要的物品,敬请期待~')
# # 持家小能手 (利用自动更新成绩程序的逻辑完成这个新项目)
# # 如果输入的水果价钱比字典中的低,则替换掉,反之则不更新
# cost= {'苹果':5.2,'山竹':12.9,'香蕉':2.4,'荔枝':15,'葡萄':9.3,'桂圆':8,'蓝莓':10,'李子':8}
# k=input('请输入水果名称:')
# v=input('请输入水果价钱:')
# if k in cost:
# if cost[k] > float(v):
# cost[k] = float(v)
# print(k+'降价了,现价'+v+'元')
# else:
# print(k+'涨价了,我们不买')
# else:
# cost[k] = float(v)
# print(k+'的价钱'+v+'元已上传~')
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment