博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
coursesa课程 Python 3 programming course_2_assessment_7 多参数函数练习题
阅读量:4056 次
发布时间:2019-05-25

本文共 3016 字,大约阅读时间需要 10 分钟。

Write a function called checkingIfIn that takes three parameters. The first is a required parameter, which should be a string. The second is an optional parameter called direction with a default value of True. The third is an optional parameter called d that has a default value of {‘apple’: 2, ‘pear’: 1, ‘fruit’: 19, ‘orange’: 5, ‘banana’: 3, ‘grapes’: 2, ‘watermelon’: 7}. Write the function checkingIfIn so that when the second parameter is True, it checks to see if the first parameter is a key in the third parameter; if it is, return True, otherwise return False.

But if the second paramter is False, then the function should check to see if the first parameter is not a key of the third. If it’s not, the function should return True in this case, and if it is, it should return False.

def checkingIfIn( x, direction = True , d = {
'apple': 2, 'pear': 1, 'fruit': 19, 'orange': 5, 'banana': 3, 'grapes': 2, 'watermelon': 7} ): lst = list(d.keys()) if direction is True: if x in lst: return True else: return False elif direction is False: if x not in lst: return True else: return Falseprint(checkingIfIn('apple'))

We have provided the function checkingIfIn such that if the first input parameter is in the third, dictionary, input parameter, then the function returns that value, and otherwise, it returns False. Follow the instructions in the active code window for specific variable assignmemts.

def checkingIfIn(a, direction = True, d = {
'apple': 2, 'pear': 1, 'fruit': 19, 'orange': 5, 'banana': 3, 'grapes': 2, 'watermelon': 7}): if direction == True: if a in d: return d[a] else: return False else: if a not in d: return True else: return d[a]# Call the function so that it returns False and assign that function call to the variable c_falsec_false = checkingIfIn('hello')# Call the fucntion so that it returns True and assign it to the variable c_truec_true = checkingIfIn('hels',False)# Call the function so that the value of fruit is assigned to the variable fruit_ansfruit_ans = checkingIfIn('fruit')# Call the function using the first and third parameter so that the value 8 is assigned to the variable param_checkparam_check = checkingIfIn('apple')*4

Write a function, test, that takes in three parameters: a required integer, an optional boolean whose default value is True, and an optional dictionary, called dict1, whose default value is {2:3, 4:5, 6:8}. If the boolean parameter is True, the function should test to see if the integer is a key in the dictionary. The value of that key should then be returned. If the boolean parameter is False, return the boolean value “False”.

def test(x,y=True,dict1={
2:3, 4:5, 6:8}): if y is True: lst = dict1.keys() for s in lst: if s == x: return dict1[s] elif y is False: return Falsefhand = int(input())print(test(fhand))

转载地址:http://iirci.baihongyu.com/

你可能感兴趣的文章
支付宝生活号服务号 用户信息获取 oauth2 登录对接 springboot java
查看>>
CodeForces #196(Div. 2) 337D Book of Evil (树形dp)
查看>>
uva 12260 - Free Goodies (dp,贪心 | 好题)
查看>>
uva-1427 Parade (单调队列优化dp)
查看>>
【设计模式】学习笔记13:组合模式(Composite)
查看>>
hdu 1011 Starship Troopers (树形背包dp)
查看>>
hdu 1561 The more, The Better (树形背包dp)
查看>>
【设计模式】学习笔记14:状态模式(State)
查看>>
poj 1976 A Mini Locomotive (dp 二维01背包)
查看>>
斯坦福大学机器学习——因子分析(Factor analysis)
查看>>
项目导入时报错:The import javax.servlet.http.HttpServletRequest cannot be resolved
查看>>
linux对于没有写权限的文件如何保存退出vim
查看>>
Windows下安装ElasticSearch6.3.1以及ElasticSearch6.3.1的Head插件
查看>>
IntelliJ IDEA 下的svn配置及使用的非常详细的图文总结
查看>>
【IntelliJ IDEA】idea导入项目只显示项目中的文件,不显示项目结构
查看>>
ssh 如何方便的切换到其他节点??
查看>>
JSP中文乱码总结
查看>>
Java-IO-File类
查看>>
Java-IO-java的IO流
查看>>
Java-IO-输入/输出流体系
查看>>