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

目录

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

描述

输入描述:

输出描述:

作答

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

描述

输入描述:

输出描述:

作答

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

描述

输入描述:

输出描述:

作答

附:本文所有题目均来自牛客网 - 找工作神器|笔试题库|面试经验|实习招聘内推,求职就业一站解决_牛客网





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

描述

又到了一年一度的牛客运动会,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


作答

  1. my_tuple = ('Tom','Andy')
  2. print('Here is the original tuple:')
  3. for name in my_tuple:
  4. print(name)
  5. print()
  6. try:
  7. my_tuple[1] = 'Allen'
  8. except TypeError:
  9. print("my_tuple[1] = 'Allen' cause a TypeError: 'tuple' object does not support item assignment")
  10. my_tuple = ('Tom','Allen')
  11. print()
  12. print('my_tuple was changed to:')
  13. for name in my_tuple:
  14. 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确实新增了一对键-值对。

输入描述:

输出描述:

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

作答

  1. operator_dict={'<':'less than','==':'equal'}
  2. print('Here is the original dict:')
  3. for x in sorted(operator_dict):
  4. print(f'Operator {x} means {operator_dict[x]}.')
  5. print(" ")
  6. operator_dict['>']='greater than'
  7. print("The dict was changed to:")
  8. for x in sorted(operator_dict):
  9. 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?

作答

  1. survey_list = ['Niumei','Niu Ke Le','GURR','LOLO']
  2. result_dict = {'Niumei':'Nowcoder','GURR': 'HUAWEI'}
  3. for i in survey_list:
  4. if i in result_dict.keys():
  5. print('Hi, ' + i +'! Thank you for participating in our graduation survey!')
  6. else:
  7. print('Hi, ' + i + '! Could you take part in our graduation survey?')


全部评论
Python入门的笔试考题
点赞 回复 分享
发布于 2022-08-27 16:58 河南

相关推荐

10-30 19:23
已编辑
山东大学(威海) C++
牛至超人:其实简历是不需要事无巨细的写的,让对方知道你有这段经历就行了,最重要的是面试的时候讲细讲明白
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务