[DHU数据科学]HW1

1. (简答题) 编写Python程序实现以下功能:从键盘输入若干同学的姓名,保存在字符串列表中;输入某个同学的名字,检索是否已保存在列表中。 

# 初始化一个空列表来存储同学的姓名
students = []

# 从键盘输入姓名
while True:
    name = input("请输入同学的姓名(输入'结束'来停止):")
    if name == '结束':
        break
    students.append(name)

# 输出已输入的同学姓名
print("已输入的同学姓名:", students)

# 持续进行姓名检索
while True:
    search_name = input("请输入要检索的同学姓名(输入'退出'来停止检索):")
    if search_name == '退出':
        print("退出检索。")
        break

    # 检索姓名是否在列表中
    if search_name in students:
        print(f"{search_name} 在列表中。")
    else:
        print(f"{search_name} 不在列表中。")

2. (简答题) 编写Python程序实现以下功能:使用字典记录多位同学的姓名及对应身高;输入任意同学的姓名,查找并显示所有高于此身高的同学信息。 

# Initialize an empty dictionary to store students' names and heights
students = {}

# Input students' names and heights
while True:
    name = input("Enter the student's name (type 'end' to stop): ")
    if name.lower() == 'end':
        break
    height = input(f"Enter {name}'s height (in centimeters): ")
    
    # Store the name and height in the dictionary
    students[name] = float(height)

# Output the entered student information
print("Entered student information:", students)

# Find students taller than the specified student
while True:
    search_name = input("Enter the name of the student to search for (type 'exit' to stop searching): ")
    if search_name.lower() == 'exit':
        print("Exiting search.")
        break

    if search_name in students:
        search_height = students[search_name]
        print(f"{search_name}'s height is {search_height} cm.")
        print("The following students are taller than them:")
        
        # Find and print other students taller than the specified student
        found = False
        for student, height in students.items():
            if height > search_height:
                print(f"{student}: {height} cm")
                found = True
        
        if not found:
            print("There are no students taller than this student.\n")
    else:
        print(f"Student {search_name} not found.\n")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Juneeeeeeeeeeeee

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值