python第六天上机练习

“”"
练习:根据输入的季节获取对应的月份
“”"

while True:
    dict_season = dict([
        ("春", (1, 2, 3)),
        ("夏", (4, 5, 6)),
        ("秋", (7, 8, 9)),
        ("冬", (10, 11, 12))
    ])
    season = input("请输入季度:")
    if season in dict_season:
        for key, values in dict_season.items():
            if key == season:
                print(str(values)+"月份")
        break
    else:
        print("输入有误!")

“”"
练习在控制台中录入多个学生的姓名、性别、成绩、年龄
“”"

dict_students_info = {}
while True:
    name = input("请输入姓名:")
    if name == "esc":
        break
    age = int(input("请输入年龄:"))
    source_num = int(input("请输入成绩科目数:"))
    sources = []
    for i in range(source_num):
        source = float(input("请录入第%d科的成绩:" % (i+1)))
        sources.append(source)
    sex = input("请录入学生的性别:")
    dict_students_info[name] = [{
        "年龄": age,
        "成绩": sources,
        "性别": sex
    }]
for key, values in dict_students_info.items():
    print(key, values)

“”"
练习:创建调查问卷
输入姓名(esc结束)
输入喜好(esc结束)
调查后显示所有信息
“”"

dict_questionnaire = {}
list_hobbys = []
while True:
    name = input("请输入姓名(esc结束):")
    if name == "esc":
        break
    else:
        while True:
            hobby = input("请输入喜好(esc结束):")
            if hobby == "esc":
                break
            else:
                list_hobbys.append(hobby)
    dict_questionnaire[name] = list_hobbys
print(dict_questionnaire)

“”"
练习
1、将1970年至2050中的闰年,存入列表
“”"

list_leap_year = []
for i in range(1970, 2051):
    if i % 4 == 0 and i % 100 != 0 or i % 400 == 0:
        list_leap_year.append(i)
    else:
        pass

“”"
练习
2、描述多个商品信息(屠龙刀:100000元,苹果:5999元)等
“”"

dict_goods = {"iphone x": 4900, "华为": 3500, "魅族16spro": 2999}
for key, values in dict_goods.items():
    print("商品名:"+key+"----------------"+"价格为:"+str(values))

“”"
练习
3、存储全国个个城市的景区与美食
北京:
景区:故宫,天安门,天坛
美食:烤鸭,炸酱面,卤煮
四川:
景区:九寨沟,峨眉山
美食:火锅,串串香,兔头
在控制台中显示
“”"

dict_city = {
                "北京": {
                        "景区": ["故宫", "天安门", "天坛"],
                        "美食": ["烤鸭", "炸酱面", "卤煮"]
                },
                "四川": {
                    "景区": ["峨眉山", "九寨沟"],
                    "美食": ["兔头", "串串香", "火锅"]
                }
             }
for city, info in dict_city.items():
    print(city)
    for key, value in info.items():
        print("%s:" % key)
        for item in value:
            print("%s" % item)

“”"
练习
4、计算一个字符串中的字符以及出现的次数
abcdeface
a 2
b 1
c 2
d 1
e 2
f 1
“”"

str_1 = "abcadefshgeghe"
dict_result = {}
for item in str_1:
    # 判断字符是否相同,相同加一
    if item not in dict_result:
        dict_result[item] = 1
    else:
        dict_result[item] += 1
print(dict_result)

“”"
猜拳游戏:石头剪刀布
系统随机选择一个
用户输入一个
判断输赢

提示:将胜利策略存入容器
石头 战胜 剪刀
剪刀 战胜 布
布  战胜  石头

“”"

import random
wins = {
    "石头": "剪刀",
    "剪刀": "布",
    "布": "石头",
}
tuple_items = ("剪刀", "布", "石头")
reando_number = random.randint(0, 2)
str_sys_input = tuple_items[reando_number]
print(str_sys_input)
user_input = input("请输入:")
if str_sys_input == user_input:
    print("平局!!")
elif wins[user_input] == str_sys_input:
    print("胜利!!")
else:
    print("失败!!")
发布了32 篇原创文章 · 获赞 7 · 访问量 7759

猜你喜欢

转载自blog.csdn.net/adim__/article/details/104077781
今日推荐