前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python入门与基础刷题篇(6)

Python入门与基础刷题篇(6)

作者头像
吉师散养基地
发布2022-11-21 14:26:48
3320
发布2022-11-21 14:26:48
举报
文章被收录于专栏:我奏是太阳

目录

题目一:牛客运动会(入门)

描述

输入描述:

输出描述:

作答

题目二:遍历字典(入门)

描述

输入描述:

输出描述:

作答

题目三:毕业生就业调查(中等)

描述

输入描述:

输出描述:

作答


题目一:牛客运动会(入门)

描述

又到了一年一度的牛客运动会,Tom和Andy报名参加了项目,

但由于比赛前一天,Andy喝了太多碳酸饮料,导致身体不适,所以临时让Allen上场了,

换人参赛需要修改参赛名单,请完成以下内容模拟整个过程。

请创建一个依次包含字符串'Tom'和'Andy'的元组my_tuple,

先使用print()语句一行打印字符串'Here is the original tuple:',再使用for循环将元组my_tuple的内容打印出来;

请使用try-except代码块执行语句my_tuple[1] = 'Allen',

若引发TypeError错误,先输出一个换行,再使用print()语句一行打印字符串"my_tuple[1] = 'Allen' cause cause a TypeError: 'tuple' object does not support item assignment";

再重新对my_tuple赋值一个新元组,新元组依次由字符串'Tom'和'Allen'构成。 输出一个换行,先使用print()语句一行打印字符串'The tuple was changed to:',再使用for循环将元组my_tuple的内容打印出来,确定修改无误。

输入描述:

输出描述:

按题目描述进行输出即可(注意前后两个输出部分需以一个空行进行分隔)。

Here is the original tuple: Tom Andy my_tuple[1] = 'Allen' cause a TypeError: 'tuple' object does not support item assignment my_tuple was changed to: Tom Allen

作答

代码语言:javascript
复制
my_tuple = ('Tom','Andy')
print('Here is the original tuple:')
for name in my_tuple:
    print(name)
print()
try:
    my_tuple[1] = 'Allen'
except TypeError:
    print("my_tuple[1] = 'Allen' cause a TypeError: 'tuple' object does not support item assignment")

my_tuple = ('Tom','Allen')
print()
print('my_tuple was changed to:')
for name in my_tuple:
    print(name)

题目二:遍历字典(入门)

描述

创建一个依次包含键-值对'<': 'less than'和'==': 'equal'的字典operators_dict,

先使用print()语句一行打印字符串'Here is the original dict:',

再使用for循环遍历 已使用sorted()函数按升序进行临时排序的包含字典operators_dict的所有键的列表,使用print()语句一行输出类似字符串'Operator < means less than.'的语句;

对字典operators_dict增加键-值对'>': 'greater than'后,

输出一个换行,再使用print()语句一行打印字符串'The dict was changed to:',

再次使用for循环遍历 已使用sorted()函数按升序进行临时排序的包含字典operators_dict的所有键的列表,使用print()语句一行输出类似字符串'Operator < means less than.'的语句,确认字典operators_dict确实新增了一对键-值对。

输入描述:

输出描述:

按题目描述进行输出即可(注意前后两个输出部分需以一个空行进行分隔)。

作答

代码语言:javascript
复制
operator_dict={'<':'less than','==':'equal'}
print('Here is the original dict:')
for x in sorted(operator_dict):
   print(f'Operator {x} means {operator_dict[x]}.')
print(" ")
operator_dict['>']='greater than'
print("The dict was changed to:")
for x in sorted(operator_dict):
   print(f'Operator {x} means {operator_dict[x]}.')

题目三:毕业生就业调查(中等)

描述

又到了毕业季,牛牛作为牛客大学的学生会主席,决定对本校的应届毕业生进行就业调查。

他创建了一个依次包含字符串'Niumei'、'Niu Ke Le'、'GURR'和'LOLO'的列表survey_list,作为调查名单;

又创建了一个依次包含键-值对'Niumei': 'Nowcoder'和'GURR': 'HUAWEI'的字典result_dict,作为已记录的调查结果。

请遍历列表survey_list,如果遍历到的名字已出现在 包含字典result_dict的全部键的列表 里,

则使用print()语句一行输出类似字符串'Hi, Niumei! Thank you for participating in our graduation survey!'的语句以表达感谢,

否则使用print()语句一行输出类似字符串'Hi, Niu Ke Le! Could you take part in our graduation survey?'的语句以发出调查邀请。

输入描述:

输出描述:

按题目描述进行输出即可。

Hi, Niumei! Thank you for participating in our graduation survey! Hi, Niu Ke Le! Could you take part in our graduation survey? Hi, GURR! Thank you for participating in our graduation survey! Hi, LOLO! Could you take part in our graduation survey?

作答

代码语言:javascript
复制
survey_list = ['Niumei','Niu Ke Le','GURR','LOLO']
result_dict = {'Niumei':'Nowcoder','GURR': 'HUAWEI'}
for i in survey_list:
    if i in result_dict.keys():
        print('Hi, ' + i +'! Thank you for participating in our graduation survey!')
    else:
        print('Hi, ' + i + '! Could you take part in our graduation survey?')
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-10-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目一:牛客运动会(入门)
  • 描述
    • 输入描述:
      • 输出描述:
        • 作答
        • 题目二:遍历字典(入门)
        • 描述
          • 输入描述:
            • 输出描述:
              • 作答
              • 题目三:毕业生就业调查(中等)
              • 描述
                • 输入描述:
                  • 输出描述:
                    • 作答
                    领券
                    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档