1、
planet_list=["水星","金星","火星","木星"]
print(planet_list)planet_list.insert(2,"地球")
print(planet_list)planet_list.append("土星")
planet_list.append("天王星")
planet_list.append("海王星")
print(planet_list)planet_list.remove("金星")
planet_list.pop()
print(planet_list)
2、找出10-100之间能被7整除的数字,乘10后再保存到列表中
data1 = []
for ch in range(10, 101):if ch % 7 == 0:data1.append(ch * 10)
print(data1)
3、
4、
from machine import Pin
import time
led_pin[15,2,0,4]
leds=[]
for i in range(0,len(led_pin)):leds.append(Pin(led_pin[i],Pin.OUT))
while True:#LED逐个点亮for i in range(0,len(leds)):leds[i].value(1)time.sleep_ms(150)#LED逐个熄灭for i in range(-1,-len(leds),-1):leds[i].value(0)time.sleep_ms(150)
5、
tuple_test = tuple(range(2, 20, 2))
print(tuple_test) # (2, 4, 6, 8, 10, 12, 14, 16, 18)
6、
dict_city={"广东省":{"广州市":["天河区","白云区","黄埔区","越秀区","东山区"],"汕头市":["濠江区","龙湖区","金平区","潮阳区","潮南区","澄海区"],"佛山市":["城区","金湾区","石湾区"],},"山东省":{"济南市":["历下区","市中区","天桥区","槐荫区","历城区","长清区","平阴县","济阳县","商河县","章丘区"],"青岛市":["市南区","市北区","李沧区","城阳区","黄岛区","崂山区"],"威海市":["环翠区","文登区"], }
}gd_list=list(dict_city.get("广东省").keys())
print(gd_list)area_count=0
for item in list(dict_city.get("广东省").values()):area_count+=len(item)
print("广东省的所有市中区的数量:",area_count)qd_area_name=tuple(dict_city.get("山东省").get("青岛市"))
print(qd_area_name)all_area_list=[]
for province in dict_city.keys():for area_list in dict_city.get(province).values():all_area_list+=area_list
print("白云区"in all_area_list)
print("钢城区"in all_area_list)# print(dict_city.get("山东省").keys())
# del dict_city["山东省"]["威海市"]
# print(dict_city.get("山东省").keys())sd=list(dict_city.get("山东省").keys())
print(sd)
del dict_city["山东省"]["威海市"]
sd1=list(dict_city.get("山东省").keys())
print(sd1)
7、
names=("小王","小张","小吴","小明")
candicate={}
for num in range(0,len(names)):candicate[str(num+1)]=[names[num],0]
print(candicate)#循环投票
while True:#打印候选者名单for key,value in candicate.items():print(key,candicate[key][0],end=' ')print()voter_choice=input("请输入您的选择(0结束):")if voter_choice=="0":breakif voter_choice not in candicate.keys():print("无效票")else:candicate[voter_choice][1]+=1print("您已给{}投票".format(candicate[voter_choice][0]))
#统计结果
winner=candicate['1']
for key in candicate:#唱票print(key,candicate[key][0],"获得了:",candicate[key][1],"票")#计算得票最多的候选者if candicate[key][1]>winner[1]:winner=candicate[key]
print("恭喜{}当选为学生会主席".format(winner[0]))
8、
9、
def is_year(year):if(year%4==0 and year%100!=0) or year%400==0:return Trueelse:return Falsefor year in range(1949,2025):if is_year(year):print(year)
10、
def cal_price(price):if price<=0:return "Invalid input"elif price>=1000:return price-90elif price>=500:return price-40elif price>=500:return price-30elif price>=100:return price-5 else:return price
print(cal_price(1000))
print(cal_price(500))
print(cal_price(300))
print(cal_price(50))
print(cal_price(-30))
11、
"""模拟双色球投注系统
"""
import randomdef get_ball():"""随机获取一注双色球号码"""list_ball = []count = 1# 随机获取6个红球号码while True:red_ball = random.randint(1,33)# 去重判断if red_ball not in list_ball:list_ball.append(red_ball)else:continue# 计数count += 1if count > 6:break# 红球排序list_ball.sort()# 获取一个蓝球blue_ball = random.randint(1,16)list_ball.append(blue_ball)return list_balldef print_ball(ball):"""打印双色球彩票号码"""print("红球:", end=" ")for i in range(0, len(ball)-1):print("%02d" % ball[i], end=" ")print("\t蓝球:%02d" % ball[6])def red_ball(ball_1,ball_2):"""计算两注双色球中有多少个相同的红球"""count = 0for index in range(0,len(ball_1)-1):if ball_1[index] in ball_2:count += 1return countdef lottery(target_ball, lottery_ball):"""根据开奖号码(lottery_ball),计算target_ball中了几等奖"""if target_ball == lottery_ball:print("一等奖")print_ball(target_ball)return "一等奖"elif target_ball[0:6] == lottery_ball[0:6]:print("二等奖")print_ball(target_ball)return "二等奖"# 5红球+1蓝球elif red_ball(target_ball, lottery_ball) == 5 and \target_ball[6] == lottery_ball[6]:print("三等奖")print_ball(target_ball)return "三等奖"# 5红球+0蓝球 或 4红球+1蓝球elif red_ball(target_ball, lottery_ball)==5 or \(red_ball(target_ball, lottery_ball)==4 and \target_ball[6]==lottery_ball[6]):print("四等奖")print_ball(target_ball)return "四等奖"# 4红球+0蓝球 或 3红球+1蓝球elif red_ball(target_ball, lottery_ball)==4 or \(red_ball(target_ball, lottery_ball)==3 and \target_ball[6]==lottery_ball[6]):print("五等奖")print_ball(target_ball)return "五等奖" # (2+1,1+1,0+1)elif target_ball[6] == lottery_ball[6]:print("六等奖")print_ball(target_ball)return "六等奖"else:return "未中奖"# 判断运行环境:如果从当前脚本运行模块名字将是__main__
# 如果被其他模块导入,模块名字将是文件名,后面代码不会执行
if __name__ == "__main__":# 定义字典,保存所有的彩票号码dict_ball = {}count = eval(input("请输入您要购买的彩票个数:"))for i in range(1, count+1):dict_ball["第%d注"%i] = get_ball()# 打印彩票for i in dict_ball:print_ball(dict_ball[i])# 获取开奖号码result = input("是否获取开奖结果:Y(是),N(否)")if result == "Y" or result == "y":lottery_ball = get_ball()print("==========================")print("本期双色球开奖号码")print_ball(lottery_ball)print("==========================")else:print("退出")exit()# 兑奖查询bonus = 0 # 记录奖金for key in dict_ball:grade = lottery(dict_ball[key], lottery_ball)if grade == "未中奖":continueelif grade == "一等奖":bonus += 5000000elif grade == "二等奖":bonus += 500000elif grade == "三等奖":bonus += 3000elif grade == "四等奖":bonus += 200elif grade == "五等奖":bonus += 10else:bonus += 5 print("您购买的彩票的总奖金是:%d元" % bonus)