Python基础小项目
今天给大家写一期特别基础的Python小项目,欢迎大家支持,并给出自己的完善修改
(因为我写的都是很基础的,运行速率不是很好的
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 = 1
j = 1
money = 0
print("== 请输入距离 或 按'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 = 1
while j <= 2:
if money < 100:
if distance <= 6:
money += 3
if distance > 6 and distance <= 12:
money += 4
if distance > 12 and distance <= 22:
money += 5
if distance > 22 and distance <= 32:
money += 6
if distance > 32:
money += ((distance - 33) // 20) + 6 + 1
elif money >= 100 and money < 150:
if distance <= 6:
money += 3 * 0.8
if distance > 6 and distance <= 12:
money += 4 * 0.8
if distance > 12 and distance <= 22:
money += 5 * 0.8
if distance > 22 and distance <= 32:
money += 6 * 0.8
if distance > 32:
money += (((distance - 33) // 20) + 6 + 1) * 0.8
elif money >= 150 and money < 400:
if distance <= 6:
money += 3 * 0.5
if distance > 6 and distance <= 12:
money += 4 * 0.5
if distance > 12 and distance <= 22:
money += 5 * 0.5
if distance > 22 and distance <= 32:
money += 6 * 0.5
if distance > 32:
money += (((distance - 33) // 20) + 6 + 1) * 0.5
elif money > 400:
if distance <= 6:
money += 3
if distance > 6 and distance <= 12:
money += 4
if distance > 12 and distance <= 22:
money += 5
if distance > 22 and distance <= 32:
money += 6
if distance > 32:
money += (((distance - 33) // 20) + 6 + 1)
j += 1
day += 1
money += 5
print("n你的总花费为:%.4f元n" % money)
elif yikatong.lower() == "n": #把输入的字母变成小写
while day <= 20:
j = 1
while j <= 2:
if distance <= 6:
money += 3
if distance > 6 and distance <= 12:
money += 4
if distance > 12 and distance <= 22:
money += 5
if distance > 22 and distance <= 32:
money += 6
if distance > 32:
money += ((distance - 33) // 20) + 6 + 1
j += 1
day += 1
money += 5
print("你的总花费为:%.4f元" % money)
else:
print("nXX 输入有误请重新输入 XX")
else:
print("必须大于0,请从新输入")
elif distance.lower() == "q":
break
else:
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 = 0
money = []
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("直接退出")
break
elif salary > 0 and salary < money[0]: # 判断输入的金额能否购买价格最低的商品
print("你的金额买不起任何一个东西n")
elif salary > money[0]:
while True:
all_price = 0
print("33[36;1m商品列表33[1m".center(40, "="))
for i, v in enumerate(goods, 1):
print("%d %s 33[34;1m%d33[1m" % (i, v["name"].ljust(4, " "), v["price"]))
print("请输入产品33[31;1m序号33[1m添加到购物车、返回上一层请按 33[31;1mP 33[1m、完全退出请按 33[31;1mQ33[1m")
seq_num = input(">>> ")
if seq_num.lower() == "p":
break
if seq_num.lower() == "q":
q = 1
break
if 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%s33[1m 已添加到购物车n" % (goods[seq_num - 1]["name"]))
while True:
print("继续添加产品请按 33[31;1mC33[1m 、结算请按 33[31;1mB33[1m 、查看购物车请按 33[31;1mS33[1m 、完全退出请按 33[31;1mQ33[1m")
final_cho = input(">>> ")
if final_cho.lower() == "c":
break
elif final_cho.lower() == "b":
while True:
print("您的余额为:33[34;1m%d33[1m 您购买的商品总价为:33[34;1m%d33[1m 确定购买吗? 确定 33[31;1mY33[1m 取消 33[31;1mN33[1m" % (salary, all_price))
confirm_bill = input(">>> ")
if confirm_bill.lower() == "y":
if shopping_cart != []:
if salary >= all_price:
salary = salary - all_price
print("购买成功n")
all_price = 0
shopping_cart = []
break
elif salary < all_price:
print("-_-!余额不足n")
break
elif shopping_cart == []:
print("购物车空空如也,填充后再来吧n")
break
elif confirm_bill.lower() == "n":
break
else:
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%d33[1m %d个" % (m, z["name"].ljust(4, " "), z["price"], shopping_cart.count(z)))
print("n购物车商品总金额为:33[34;1m%d33[1m" % all_price)
print("您的余额为:33[34;1m%d33[1m" % salary)
print("按33[31;1m序号33[1m可删除商品 、继续请按 33[31;1mC33[1m 、清空购物车请按 33[31;1mE33[1m")
ctrl_shop_cart = input(">>> ")
if ctrl_shop_cart.lower() == "c":
break
elif ctrl_shop_cart.lower() == "e":
all_price = 0
shopping_cart = []
print("以清空购物车")
break
elif 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 = 1
break
else:
print("输入有误,请重新输入n")
else:
print("数字超出范围,请重新输入n")
else:
print("请输入数字n")
if q == 1:
break
else:
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"] = name
dic_one["age"] = age
dic_one["stu_num"] = stu_num
dic_one["weixin"] = weixin
all_li.append(dic_one)
print("==添加成功==")
elif first_num == 2:
while True:
print("n==请输入要删除的名字==")
del_name = input(">>> ")
count = 0
f = 0
for i in all_li:
count += 1
if i["name"] == del_name:
count -= 1
f = 1
break
if f == 0:
print("找不到你要找的名字,请重新输入")
continue
del all_li[count]
print("删除成功")
break
elif first_num == 3:
while True:
print("请输入要修改的名字")
mod_name = input(">>> ")
count = 0
f = 0
for i in all_li:
count += 1
if i["name"] == mod_name:
count -= 1
f = 1
if f == 0:
print("找不到你要找的名字,请重新输入")
continue
print("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_name2
print("修改成功")
break
if mod_num == 2:
print("n请输入%s的新年龄" % all_li[count]["name"])
mod_age = input(">>> ")
all_li[count]["age"] = mod_age
print("修改成功")
break
if mod_num == 3:
print("n请输入%s的新学号" % all_li[count]["name"])
mod_stu_num = input(">>> ")
all_li[count]["stu_num"] = mod_stu_num
print("修改成功")
break
if mod_num == 3:
print("n请输入%s的新微信" % all_li[count]["name"])
mod_weixin = input(">>> ")
all_li[count]["weixin"] = mod_weixin
print("修改成功")
break
else:
print("输入有误,重新输入")
break
elif first_num == 4:
while True:
print("n==请输入要查找的名字==")
c = 0
find_name = input(">>> ")
for i in all_li:
if i["name"] == find_name:
print("名字tt年龄tt学号tt微信")
print("%stt%stt%stt%s"%(i["name"],i["age"],i["stu_num"],i["weixin"]))
else:
print("找不到你要找的名字请重新输入")
c = 1
if c == 1:
continue
break
elif first_num == 5:
result = "名字t年龄t学号t微信"
print(result.expandtabs(20))
for i in all_li:
result2 = "%st%st%st%s"%(i["name"],i["age"],i["stu_num"],i["weixin"])
print(result2.expandtabs(20))
elif first_num == 6:
break
else:
print("输入超出范围,请重新输入")
else:
print("输入有误,请重新输入")
运行截图
4. 用户交换显示
题目
比如有如下形式的字典
city = {“北京”: {“朝阳”: [“望京”, “大望路”], “昌平”: [“沙河”, “小昌平”]},
“延边”: {“延吉”: [“北大”, “铁南”], “龙井”: [“图们”, “珲春”]},
“上海”: {“新上海”: [“浦东”, “浦西”], “老上海”: [“上海滩”, “不夜城”]}}
完成以下要求:
允许用户增加内容
允许用户查看某一个级别的内容
即可查看内容也可修改内容
也可删除内容
每一级的循环嵌套必须包含返回上一层,也包含全部退出
程序源码
city = {"北京": {"朝阳": ["望京", "大望路"], "昌平": ["沙河", "小昌平"]},
"延边": {"延吉": ["北大", "铁南"], "龙井": ["图们", "珲春"]},
"上海": {"新上海": ["浦东", "浦西"], "老上海": ["上海滩", "不夜城"]}}
while True:
p = 0
q = 0
print("33[35;1m一级列表33[1m".center(50,"="))
for i,v in enumerate(city,1):
print(i,v)
print("添加请按33[31;1m A33[1m 、查看请按33[31;1m 序号33[1m 、退出请按33[31;1m Q33[1m")
init_cho = input(">>> ")
if init_cho.lower() == "q":
break
elif 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%s33[1m请按33[31;1m Y33[1m 、修改请按33[31;1m M33[1m 、返回上一级请按33[31;1m P33[1m 、进入下一级请按33[31;1m N33[1m 、全部退出请按33[31;1m Q33[1m"% first_li[init_cho-1])
deci_cho = input(">>> ")
if deci_cho.lower() == "y":
del city[first_li[init_cho-1]]
print("删除成功,自动跳回上一级")
break
elif deci_cho.lower() == "q":
q = 1
break
elif deci_cho.lower() == "p":
break
elif 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 = 0
second_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 P33[1m 、全部退出请按33[31;1m Q33[1m")
sec_cho = input(">>> ")
if sec_cho.lower() == "p":
p = 1
break
elif sec_cho.lower() == "q":
q = 1
break
elif 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%s33[1m请按33[31;1m Y33[1m 、修改请按33[31;1m M33[1m 、返回上一级请按33[31;1m P33[1m 、进入下一级请按33[31;1m N33[1m 、全部退出请按33[31;1m Q33[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("删除成功,自动跳回上一级")
break
elif 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("修改成功,自动跳回上一级")
break
elif third_cho.lower() == "p":
break
elif third_cho.lower() == "q":
q = 1
break
elif 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;1mA33[1m 、返回上一级请按33[31;1m P33[1m 、全部退出请按33[31;1m Q33[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;1mD33[1m 、修改请按 33[31;1mM33[1m 、返回上一级请按33[31;1m P33[1m 、全部退出请按33[31;1m Q33[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("删除成功,自动跳回上一层")
break
elif one_more_cho.lower() == "m":
print("您要改成什么名字?")
mod_last = input(">>> ")
city[first_li[init_cho - 1]][second_li[sec_cho - 1]][last_cho - 1] = mod_last
print("修改成功,自动跳回上一层")
break
elif one_more_cho.lower() == "p":
break
elif one_more_cho.lower() == "q":
q = 1
break
else:
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 = 1
break
elif last_cho.lower() == "q":
q = 1
break
if q == 1:
break
else:
print("输入有误,请重新输入n")
if q == 1:
break
if p2 == 1:
break
else:
print("输入超出范围,请重新输入n")
else:
print("输入有误请重新输入n")
if q == 1:
break
if q == 1:
break
if p == 1:
break
else:
print("输入超出范围,请重新输入n")
elif init_cho.lower() == "a":
while True:
print("33[36;1m添加信息33[1m".center(50, "="))
print("请输入省/直辖市、返回上一级请按33[31;1m P33[1m 、全部退出请按33[31;1m Q33[1m")
add_sheng = input(">>> ")
if add_sheng.lower() == "p":
break
elif add_sheng.lower() == "q":
q = 1
break
elif city.get(add_sheng,0) == 0:
city.update({add_sheng:{}})
print("添加成功n")
print("请输入市/州、返回上一级请按33[31;1m P33[1m 、全部退出请按33[31;1m Q33[1m")
add_shi = input(">>> ")
if add_shi.lower() == "p":
break
elif add_shi.lower() == "q":
q = 1
break
elif city[add_sheng].get(add_shi,0) == 0:
city[add_sheng].update({add_shi:[]})
print("添加成功n")
while True:
print("请输入县/街道、可多次写入、返回上一级请按33[31;1m P33[1m 、全部退出请按33[31;1m Q33[1m")
add_xian = input(">>> ")
if add_xian.lower() == "p":
break
if add_xian.lower() == "q":
q = 1
break
city[add_sheng][add_shi].append(add_xian)
print("添加成功")
if q == 1:
break
elif init_cho == "5":
print(city)
else:
print("输入有误,请重新输入n")
if q == 1:
break
运行截图
总结
目前这四个小程序写的都是很简单的,用基础的循环嵌套,实现基础的效果,希望对大家有所帮助,同时也是欢迎大家交流探讨,继续优化完善这个程序。