今天给大家写一期特别基础的Python小项目,欢迎大家支持,并给出自己的完善修改
(因为我写的都是很基础的,运行速率不是很好的
目录
- 1. 地铁票价
- 题目
- 程序源码
- 运行截图
- 2. 购物车
- 题目
- 程序源码
- 运行截图
- 3. 名片管理器
- 题目
- 程序源码
- 运行截图
- 4. 用户交换显示
- 题目
- 程序源码
- 运行截图
- 总结
1. 地铁票价
题目
地铁票价
地铁交通价格调整为:6公里(含)内3元;6公里至12公里(含)4元;12公里至22公里(含)5元;22公里至32公里(含)6元;32公里以上部分,每增加1元可乘坐20公里。
使用市政交通一卡通刷卡乘坐轨道交通,每自然月内每张卡支出累计满100元以后的乘次,价格给予8折优惠;满150元以后的乘次,价格给予5折优惠;
支出累计达到400元以后的乘次,不再享受打折优惠。
要求:
假设每个月,小明都需要上20天班,每次上班需要来回1次,即每天需要乘坐2次同样路线的地铁;
每月月初小明第一次刷公交卡时,扣款5元;
编写程序,从键盘获取距离,帮小明计算,如果不使用市政交通一卡通的每月总费用,和使用市政交通一卡通的每月总费用。
这个我写的时候用了巨多的if嵌套,效率巨低
程序源码
# 使用巨多if嵌套,效率很低
while True:day = 1j = 1money = 0print("== 请输入距离 或 按'q'退出 ==")distance = input(">>> ") # 设置距离if distance.isdecimal():distance = int(distance)if distance > 0:print("\n== 是不是一卡通? 'y'是 'n'不是==")yikatong = input(">>>[y/n] ")if yikatong.lower() == "y": #把输入的字母变成小写while day <= 20:j = 1while j <= 2:if money < 100:if distance <= 6:money += 3if distance > 6 and distance <= 12:money += 4if distance > 12 and distance <= 22:money += 5if distance > 22 and distance <= 32:money += 6if distance > 32:money += ((distance - 33) // 20) + 6 + 1elif money >= 100 and money < 150:if distance <= 6:money += 3 * 0.8if distance > 6 and distance <= 12:money += 4 * 0.8if distance > 12 and distance <= 22:money += 5 * 0.8if distance > 22 and distance <= 32:money += 6 * 0.8if distance > 32:money += (((distance - 33) // 20) + 6 + 1) * 0.8elif money >= 150 and money < 400:if distance <= 6:money += 3 * 0.5if distance > 6 and distance <= 12:money += 4 * 0.5if distance > 12 and distance <= 22:money += 5 * 0.5if distance > 22 and distance <= 32:money += 6 * 0.5if distance > 32:money += (((distance - 33) // 20) + 6 + 1) * 0.5elif money > 400:if distance <= 6:money += 3if distance > 6 and distance <= 12:money += 4if distance > 12 and distance <= 22:money += 5if distance > 22 and distance <= 32:money += 6if distance > 32:money += (((distance - 33) // 20) + 6 + 1)j += 1day += 1money += 5print("\n你的总花费为:%.4f元\n" % money)elif yikatong.lower() == "n": #把输入的字母变成小写while day <= 20:j = 1while j <= 2:if distance <= 6:money += 3if distance > 6 and distance <= 12:money += 4if distance > 12 and distance <= 22:money += 5if distance > 22 and distance <= 32:money += 6if distance > 32:money += ((distance - 33) // 20) + 6 + 1j += 1day += 1money += 5print("你的总花费为:%.4f元" % money)else:print("\nXX 输入有误请重新输入 XX")else:print("必须大于0,请从新输入")elif distance.lower() == "q":breakelse:print("必须是数字,而且大于0,请从新输入")
运行截图
2. 购物车
题目
购物车
如下商品列表
goods = [
{“name”: “电脑”, “price”: 1000},
{“name”: “Iphone”, “price”: 1200},
{“name”: “豪车”, “price”: 3280},
{“name”: “别墅”, “price”: 6500},
{“name”: “游艇”, “price”: 5800},
{“name”: “美女”, “price”: 2500},
]
完成以下要求:
要求用户输入总资产,列入:15000
判断驶入的金额能否购买价格最低的商品,如果不能让用户重新输入
显示商品列表,让用户根据序号选择商品,加入购物车
查看购物车,购物车内如果有相同的产品不要重复显示,以产品后面加数字形式表示此产品在购物车有两个或两个以上
结算时,判断购物车是否为空,如果为空则提示填充购物车
可以让用户删除购物车内的产品,或清空购物车
结算时如果余额不足,则提示账户余额不足
程序源码
goods = [{"name": "电脑", "price": 1000},{"name": "Iphone", "price": 1200},{"name": "豪车", "price": 3280},{"name": "别墅", "price": 6500},{"name": "游艇", "price": 5800},{"name": "美女", "price": 2500},
]
while True:q = 0money = []shopping_cart = []for i, v in enumerate(goods, 1):money.append(v["price"])print("请输入您的总金额")salary = input(">>> ")if salary.isdecimal(): # 判断只让输入十进制数字salary = int(salary)money.sort()if salary <= 0:print("\n你玩我呢,没钱还来买\n")print("直接退出")breakelif salary > 0 and salary < money[0]: # 判断输入的金额能否购买价格最低的商品print("你的金额买不起任何一个东西\n")elif salary > money[0]:while True:all_price = 0print("\33[36;1m商品列表\33[1m".center(40, "="))for i, v in enumerate(goods, 1):print("%d %s \33[34;1m%d\33[1m" % (i, v["name"].ljust(4, " "), v["price"]))print("请输入产品\33[31;1m序号\33[1m添加到购物车、返回上一层请按 \33[31;1mP \33[1m、完全退出请按 \33[31;1mQ\33[1m")seq_num = input(">>> ")if seq_num.lower() == "p":breakif seq_num.lower() == "q":q = 1breakif seq_num.isdecimal():seq_num = int(seq_num)if seq_num > 0 and seq_num <= len(goods):shopping_cart.append(goods[seq_num - 1])for y in shopping_cart:all_price += y["price"]print("\33[33;1m%s\33[1m 已添加到购物车\n" % (goods[seq_num - 1]["name"]))while True:print("继续添加产品请按 \33[31;1mC\33[1m 、结算请按 \33[31;1mB\33[1m 、查看购物车请按 \33[31;1mS\33[1m 、完全退出请按 \33[31;1mQ\33[1m")final_cho = input(">>> ")if final_cho.lower() == "c":breakelif final_cho.lower() == "b":while True:print("您的余额为:\33[34;1m%d\33[1m 您购买的商品总价为:\33[34;1m%d\33[1m 确定购买吗? 确定 \33[31;1mY\33[1m 取消 \33[31;1mN\33[1m" % (salary, all_price))confirm_bill = input(">>> ")if confirm_bill.lower() == "y":if shopping_cart != []:if salary >= all_price:salary = salary - all_priceprint("购买成功\n")all_price = 0shopping_cart = []breakelif salary < all_price:print("-_-!余额不足\n")breakelif shopping_cart == []:print("购物车空空如也,填充后再来吧\n")breakelif confirm_bill.lower() == "n":breakelse:print("输入有误,请重新输入\n")elif final_cho.lower() == "s":while True:print("\33[35;1m购物车\33[1m".center(40, "="))temp_cart = []for y in shopping_cart:if y not in temp_cart:temp_cart.append(y)for m, z in enumerate(temp_cart, 1):print("%d %s \33[34;1m%d\33[1m %d个" % (m, z["name"].ljust(4, " "), z["price"], shopping_cart.count(z)))print("\n购物车商品总金额为:\33[34;1m%d\33[1m" % all_price)print("您的余额为:\33[34;1m%d\33[1m" % salary)print("按\33[31;1m序号\33[1m可删除商品 、继续请按 \33[31;1mC\33[1m 、清空购物车请按 \33[31;1mE\33[1m")ctrl_shop_cart = input(">>> ")if ctrl_shop_cart.lower() == "c":breakelif ctrl_shop_cart.lower() == "e":all_price = 0shopping_cart = []print("以清空购物车")breakelif ctrl_shop_cart.isdecimal():ctrl_shop_cart = int(ctrl_shop_cart)if ctrl_shop_cart > 0 and ctrl_shop_cart <= len(temp_cart):all_price = all_price - temp_cart[ctrl_shop_cart - 1]["price"]shopping_cart.reverse()shopping_cart.remove(temp_cart[ctrl_shop_cart - 1])shopping_cart.reverse()print("删除成功\n")else:print("输入超出范围,请重新输入")else:print("输入有误,请重新输入\n")elif final_cho.lower() == "q":q = 1breakelse:print("输入有误,请重新输入\n")else:print("数字超出范围,请重新输入\n")else:print("请输入数字\n")if q == 1:breakelse:print("\n只能输入数字,请重新输入\n")if q == 1:break
运行截图
3. 名片管理器
题目
名片管理器
需要完成的基本功能:
添加名片
删除名片
修改名片
查询名片
退出系统
程序运行后,除非选择退出系统,否则重复执行功能。
程序源码
print("=" * 20)
print("==\t学生名片管理系统")
print("1:添加名片")
print("2:删除名片")
print("3:修改名片")
print("4:查找名片")
print("5:显示名片")
print("6:退出")
print("=" * 20)
all_li = []
while True:print("\n==请输入序号==")first_num = input(">>> ")if first_num.isdecimal():first_num = int(first_num)if first_num == 1:dic_one = {}print("请输入要添加的名字")name = input(">>> ")print("请输入%s的年龄" % name )age = input(">>>")print("请输入%s的学号" % name)stu_num = input(">>> ")print("请输入%s的微信" % name)weixin = input(">>> ")dic_one["name"] = namedic_one["age"] = agedic_one["stu_num"] = stu_numdic_one["weixin"] = weixinall_li.append(dic_one)print("==添加成功==")elif first_num == 2:while True:print("\n==请输入要删除的名字==")del_name = input(">>> ")count = 0f = 0for i in all_li:count += 1if i["name"] == del_name:count -= 1f = 1breakif f == 0:print("找不到你要找的名字,请重新输入")continuedel all_li[count]print("删除成功")breakelif first_num == 3:while True:print("请输入要修改的名字")mod_name = input(">>> ")count = 0f = 0for i in all_li:count += 1if i["name"] == mod_name:count -= 1f = 1if f == 0:print("找不到你要找的名字,请重新输入")continueprint("\n==名字已找到,想修改对应此名的哪项选项==")print("1:名字 2:年龄 3:学号 4:微信")while True:print("== 请输入对应的序号 ==")mod_num = input(">>> ")if mod_num.isdecimal():mod_num = int(mod_num)if mod_num == 1:print("\n请输入你要修改的名字")mod_name2 = input(">>> ")all_li[count]["name"] = mod_name2print("修改成功")breakif mod_num == 2:print("\n请输入%s的新年龄" % all_li[count]["name"])mod_age = input(">>> ")all_li[count]["age"] = mod_ageprint("修改成功")breakif mod_num == 3:print("\n请输入%s的新学号" % all_li[count]["name"])mod_stu_num = input(">>> ")all_li[count]["stu_num"] = mod_stu_numprint("修改成功")breakif mod_num == 3:print("\n请输入%s的新微信" % all_li[count]["name"])mod_weixin = input(">>> ")all_li[count]["weixin"] = mod_weixinprint("修改成功")breakelse:print("输入有误,重新输入")breakelif first_num == 4:while True:print("\n==请输入要查找的名字==")c = 0find_name = input(">>> ")for i in all_li:if i["name"] == find_name:print("名字\t\t年龄\t\t学号\t\t微信")print("%s\t\t%s\t\t%s\t\t%s"%(i["name"],i["age"],i["stu_num"],i["weixin"]))else:print("找不到你要找的名字请重新输入")c = 1if c == 1:continuebreakelif first_num == 5:result = "名字\t年龄\t学号\t微信"print(result.expandtabs(20))for i in all_li:result2 = "%s\t%s\t%s\t%s"%(i["name"],i["age"],i["stu_num"],i["weixin"])print(result2.expandtabs(20))elif first_num == 6:breakelse:print("输入超出范围,请重新输入")else:print("输入有误,请重新输入")
运行截图
4. 用户交换显示
题目
比如有如下形式的字典
city = {“北京”: {“朝阳”: [“望京”, “大望路”], “昌平”: [“沙河”, “小昌平”]},
“延边”: {“延吉”: [“北大”, “铁南”], “龙井”: [“图们”, “珲春”]},
“上海”: {“新上海”: [“浦东”, “浦西”], “老上海”: [“上海滩”, “不夜城”]}}
完成以下要求:
允许用户增加内容
允许用户查看某一个级别的内容
即可查看内容也可修改内容
也可删除内容
每一级的循环嵌套必须包含返回上一层,也包含全部退出
程序源码
city = {"北京": {"朝阳": ["望京", "大望路"], "昌平": ["沙河", "小昌平"]},"延边": {"延吉": ["北大", "铁南"], "龙井": ["图们", "珲春"]},"上海": {"新上海": ["浦东", "浦西"], "老上海": ["上海滩", "不夜城"]}}while True:p = 0q = 0print("\33[35;1m一级列表\33[1m".center(50,"="))for i,v in enumerate(city,1):print(i,v)print("添加请按\33[31;1m A\33[1m 、查看请按\33[31;1m 序号\33[1m 、退出请按\33[31;1m Q\33[1m")init_cho = input(">>> ")if init_cho.lower() == "q":breakelif init_cho.isdecimal():init_cho = int(init_cho)if init_cho > 0 and init_cho <= len(city):while True:first_li = []for i, v in enumerate(city, 1):first_li.append(v)print("删除\33[31;1m%s\33[1m请按\33[31;1m Y\33[1m 、修改请按\33[31;1m M\33[1m 、返回上一级请按\33[31;1m P\33[1m 、进入下一级请按\33[31;1m N\33[1m 、全部退出请按\33[31;1m Q\33[1m"% first_li[init_cho-1])deci_cho = input(">>> ")if deci_cho.lower() == "y":del city[first_li[init_cho-1]]print("删除成功,自动跳回上一级")breakelif deci_cho.lower() == "q":q = 1breakelif deci_cho.lower() == "p":breakelif deci_cho.lower() == "m":print("您要改成什么名字?")mod_sheng_name = input(">>>")if mod_sheng_name.isalpha():city[mod_sheng_name] = city.pop(first_li[init_cho-1])print("修改成功")elif deci_cho.lower() == "n":while True:p2 = 0second_li = []print("\33[34;1m二级列表\33[1m".center(50,"="))for x, y in enumerate(city[first_li[init_cho-1]], 1):print(x, y)second_li.append(y)print("请按\33[31;1m序号\33[1m选择、返回上一级请按\33[31;1m P\33[1m 、全部退出请按\33[31;1m Q\33[1m")sec_cho = input(">>> ")if sec_cho.lower() == "p":p = 1breakelif sec_cho.lower() == "q":q = 1breakelif sec_cho.isdecimal():sec_cho = int(sec_cho)if sec_cho > 0 and sec_cho <= len(city[first_li[init_cho - 1]]):while True:print("删除\33[31;1m%s\33[1m请按\33[31;1m Y\33[1m 、修改请按\33[31;1m M\33[1m 、返回上一级请按\33[31;1m P\33[1m 、进入下一级请按\33[31;1m N\33[1m 、全部退出请按\33[31;1m Q\33[1m"% second_li[sec_cho-1])third_cho = input(">>> ")if third_cho.lower() == "y":del city[first_li[init_cho-1]][second_li[sec_cho-1]]print("删除成功,自动跳回上一级")breakelif third_cho.lower() == "m":print("您要改成什么名字?")mod_shi_name = input(">>>")city[first_li[init_cho - 1]][mod_shi_name] = city[first_li[init_cho - 1]].pop(second_li[sec_cho-1])print("修改成功,自动跳回上一级")breakelif third_cho.lower() == "p":breakelif third_cho.lower() == "q":q = 1breakelif third_cho.lower() == "n":while True:print("\33[36;1m三级列表\33[1m".center(50, "="))for t, n in enumerate(city[first_li[init_cho - 1]][second_li[sec_cho - 1]], 1):print(t, n)print("请按\33[31;1m序号\33[1m选择删除或修改、添加请按 \33[31;1mA\33[1m 、返回上一级请按\33[31;1m P\33[1m 、全部退出请按\33[31;1m Q\33[1m")last_cho = input(">>> ")if last_cho.isdecimal():last_cho = int(last_cho)while True:if last_cho > 0 and last_cho <= len(city[first_li[init_cho - 1]][second_li[sec_cho - 1]]):print("删除请按 \33[31;1mD\33[1m 、修改请按 \33[31;1mM\33[1m 、返回上一级请按\33[31;1m P\33[1m 、全部退出请按\33[31;1m Q\33[1m")one_more_cho = input(">>> ")if one_more_cho.lower() == "d":del city[first_li[init_cho - 1]][second_li[sec_cho - 1]][last_cho - 1]print("删除成功,自动跳回上一层")breakelif one_more_cho.lower() == "m":print("您要改成什么名字?")mod_last = input(">>> ")city[first_li[init_cho - 1]][second_li[sec_cho - 1]][last_cho - 1] = mod_lastprint("修改成功,自动跳回上一层")breakelif one_more_cho.lower() == "p":breakelif one_more_cho.lower() == "q":q = 1breakelse:print("输入有误,请重新输入\n")else:print("输入超出范围,请重新输入\n")elif last_cho.lower() == "a":print("请输入添加的名")add_last = input(">>> ")city[first_li[init_cho - 1]][second_li[sec_cho - 1]].append(add_last)print("添加成功")elif last_cho.lower() == "p":p2 = 1breakelif last_cho.lower() == "q":q = 1breakif q == 1:breakelse:print("输入有误,请重新输入\n")if q == 1:breakif p2 == 1:breakelse:print("输入超出范围,请重新输入\n")else:print("输入有误请重新输入\n")if q == 1:breakif q == 1:breakif p == 1:breakelse:print("输入超出范围,请重新输入\n")elif init_cho.lower() == "a":while True:print("\33[36;1m添加信息\33[1m".center(50, "="))print("请输入省/直辖市、返回上一级请按\33[31;1m P\33[1m 、全部退出请按\33[31;1m Q\33[1m")add_sheng = input(">>> ")if add_sheng.lower() == "p":breakelif add_sheng.lower() == "q":q = 1breakelif city.get(add_sheng,0) == 0:city.update({add_sheng:{}})print("添加成功\n")print("请输入市/州、返回上一级请按\33[31;1m P\33[1m 、全部退出请按\33[31;1m Q\33[1m")add_shi = input(">>> ")if add_shi.lower() == "p":breakelif add_shi.lower() == "q":q = 1breakelif city[add_sheng].get(add_shi,0) == 0:city[add_sheng].update({add_shi:[]})print("添加成功\n")while True:print("请输入县/街道、可多次写入、返回上一级请按\33[31;1m P\33[1m 、全部退出请按\33[31;1m Q\33[1m")add_xian = input(">>> ")if add_xian.lower() == "p":breakif add_xian.lower() == "q":q = 1breakcity[add_sheng][add_shi].append(add_xian)print("添加成功")if q == 1:breakelif init_cho == "5":print(city)else:print("输入有误,请重新输入\n")if q == 1:break
运行截图
总结
目前这四个小程序写的都是很简单的,用基础的循环嵌套,实现基础的效果,希望对大家有所帮助,同时也是欢迎大家交流探讨,继续优化完善这个程序。