嵌入式实训day3

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":
        break
    if voter_choice not in candicate.keys():
        print("无效票")
    else:
        candicate[voter_choice][1]+=1
        print("您已给{}投票".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 True
    else:
        return False
    
for 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-90
    elif price>=500:
        return price-40
    elif price>=500:
        return price-30
    elif 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 random

def 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 += 1
        if count > 6:
            break
        
    # 红球排序
    list_ball.sort()
    
    # 获取一个蓝球
    blue_ball = random.randint(1,16)
    list_ball.append(blue_ball)
    
    return list_ball
    
def 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 = 0
    for index in range(0,len(ball_1)-1):
        if ball_1[index] in ball_2:
            count += 1
    return count

def 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 == "未中奖":
            continue
        elif grade == "一等奖":
            bonus += 5000000
        elif grade == "二等奖":
            bonus += 500000
        elif grade == "三等奖":
            bonus += 3000
        elif grade == "四等奖":
            bonus += 200
        elif grade == "五等奖":
            bonus += 10
        else:
            bonus += 5   
    
    print("您购买的彩票的总奖金是:%d元" % bonus)







  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值