编程导论作业答案.docx

  1. 1、本文档共5页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
编程导论作业答案

第二章 Python 介绍2.1 编写一个程序,处理三个变量X、Y和Z,然后输出他们中最大的奇数,如果没有奇数输出一段文字来说明:return the biggest odd number in a number listdef return_biggest_number(A_LIST): odd=[] for each_num in A_LIST: if each_num%2==1: odd.append(each_num) if odd==None: print(no odd number is found) return sorted(odd)[-1]print(return_biggest_number([5, 11, 3]))2.2 编写一个程序,要求用户输入10个整数,然后输出其中最大的奇数。如果没有输入奇数,输出一条信息来说明:def return_biggest_number(A_LIST): odd=[] for each_num in A_LIST: if each_num%2==1: odd.append(each_num) if odd==None: print(no odd number is found) return sorted(odd)[-1]num_list=[]for each_num in range(10): num=input(input the +str(each_num)+th number) num_list.append(int(num))print(return_biggest_number(num_list))第三章 一些简单的数值类程序3.1 编写一个程序,要求用户输入一个整数并输出两个整数root和pwr,满足0pwr6并且rootpwr 和用户输入的整数相等。如果不存在满足条件的整数对,输出一条信息来说明。(肯定存在的好吗= =!)def return_abs_root(x,pwr): ans=0 while ans**pwrabs(x): ans=ans+1 if ans**pwr!=abs(x): return -1 else: return ans def return_root_pwr(x): ans_list=[] for pwr_num in range(5): pwr_num+=1#pwr_num should not be 0 ,it can cause a disaster! abs_root=return_abs_root(x,pwr_num) if abs_root !=-1: if x0 and pwr_num%2==0: ans_list.append((abs_root,pwr_num)) ans_list.append((-abs_root,pwr_num)) else: if x0and pwr_num%2==1: ans_list.append((abs_root,pwr_num)) if x0and pwr_num%2==1: ans_list.append((-abs_root,pwr_num)) return ans_listx=int(input(input a integer))print(return_root_pwr(x))3.2 假设S是包含多个小数的字符串,小数之间使用逗号分割,例如,s=’1.23,2.4,3.123’。编写一个程序,输出s中的数字的和:def sum_string(a_str): str_list=a_str.strip().split(,) float_list=[float(each_item) for each_item in str_list] ans=0 for each_num in float_list: ans+=each_num return ansprint(sum_string(3.23,2.4,3.123))3.3 如果把图3-4中的语句x=25改成x=-25会发生什么?(自己敲代码试试,试之前请先把电脑该保存的东西都保存一下!= =!)3.4 如果要让图3-4中的代码既可以寻找负数的立方根又可以寻找正数平方根,该如何进行修改?def dichotomy(x,epsilon=0.01): low=min(x,-1.0) high=max(x,1.0) ans=(low+high)/2.0 while abs(ans**3-x)=epsilon**3: if ans**3-x0: high=ans else: low=ans ans=(low+high)/2.0 return ansprint(dichotomy(-27))3.5 二进制数10011对应的十进制数是多少

文档评论(0)

xcs88858 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

版权声明书
用户编号:8130065136000003

1亿VIP精品文档

相关文档