当前位置: 首页 > news >正文

陕西省住房城乡建设厅网站网络营销师证书怎么考

陕西省住房城乡建设厅网站,网络营销师证书怎么考,wordpress 文章 列表,创建网站时间代码题目如下 编写一个名为collatz()的函数,它有一个名为number的参数。如果参数是偶数,那么collatz()就打印出number // 2,并返回该值。如果number是奇数,collatz()就打印并返回3 * number 1。 然后编写一个程序,让用户…

题目如下

  1. 编写一个名为collatz()的函数,它有一个名为number的参数。如果参数是偶数,那么collatz()就打印出number // 2,并返回该值。如果number是奇数,collatz()就打印并返回3 * number + 1。

  2. 然后编写一个程序,让用户输入一个整数,并不断对这个数调用collatz(),直到函数返回值1

练习过程

先完成子函数的内容

def (number):if number%2 == 0:print(number//2)else:print(3*number+1)

%是求余数,如果余数为0,表示偶数;否则为奇数

//是除法运算,结果为整型;而/的结果是浮点型

举个例子说明

print("\'//\' result:" + str(10//2))
print("\'/\' result:" + str(10/2))

结果为

然后开始实现第二步

首先先写一个让用户可以输入一个整数

print("Please input a number:")
num = int(input())

int将输入的数字强制转换为整型

题目要求不断调用collatz()函数,直到返回值为1

考虑使用 while循环,不满足条件就一直循环

由于需要判断子函数返回值是否为1,因此需要在子函数中增加return(PS:如果子函数没 return,默认返回NONE)

同样举例说明

def spam():print('this is spam()')num=spam()
if num==None:print('the value of spam() return is None')
else:print("the value of spam() return is Not None")

结果如下

因此,第一版解决方法如下

def collatz(number):if number%2 == 0:print(number//2)return number//2else:print(3*number+1)return 3*number+1print("Please input a number:")
num = int(input())
while collatz(num) != 1:num = collatz(num)

结果如下

奇怪的是,每次结果都打印了两次

从头开始梳理代码,怀疑是在语句 while collatz(num) != 1:处出现的问题,这里多调用了一次子函数

更改一下

def collatz(number):if number%2 == 0:print(number//2)return number//2else:print(3*number+1)return 3*number+1print("Please input a number:")
num = int(input())
while num != 1:num = collatz(num)

结果正常


参考:

Python自动化


文章转载自:
http://gangtok.bwmq.cn
http://thammuz.bwmq.cn
http://deintegro.bwmq.cn
http://swati.bwmq.cn
http://nongraduate.bwmq.cn
http://sightline.bwmq.cn
http://edaphology.bwmq.cn
http://euthanatize.bwmq.cn
http://businesslike.bwmq.cn
http://crane.bwmq.cn
http://shovelful.bwmq.cn
http://serge.bwmq.cn
http://superlatively.bwmq.cn
http://affirmable.bwmq.cn
http://tauranga.bwmq.cn
http://feria.bwmq.cn
http://novachord.bwmq.cn
http://sandglass.bwmq.cn
http://misnomer.bwmq.cn
http://shikar.bwmq.cn
http://praties.bwmq.cn
http://cryocable.bwmq.cn
http://fixature.bwmq.cn
http://harmless.bwmq.cn
http://hippiatrist.bwmq.cn
http://intrepidity.bwmq.cn
http://unregenerate.bwmq.cn
http://ladyfinger.bwmq.cn
http://liverpudlian.bwmq.cn
http://amole.bwmq.cn
http://nextel.bwmq.cn
http://taut.bwmq.cn
http://cacotrophia.bwmq.cn
http://muhammadan.bwmq.cn
http://delusory.bwmq.cn
http://oogamy.bwmq.cn
http://lambda.bwmq.cn
http://mottramite.bwmq.cn
http://inaccessibly.bwmq.cn
http://fluidize.bwmq.cn
http://busby.bwmq.cn
http://chamade.bwmq.cn
http://fetish.bwmq.cn
http://hilloa.bwmq.cn
http://leathercoat.bwmq.cn
http://tephroite.bwmq.cn
http://props.bwmq.cn
http://polariscope.bwmq.cn
http://tychism.bwmq.cn
http://plantigrade.bwmq.cn
http://anaerobiosis.bwmq.cn
http://observance.bwmq.cn
http://comber.bwmq.cn
http://semidemisemiquaver.bwmq.cn
http://warsong.bwmq.cn
http://earhole.bwmq.cn
http://termite.bwmq.cn
http://newsdealer.bwmq.cn
http://clamatorial.bwmq.cn
http://kurdistan.bwmq.cn
http://appointed.bwmq.cn
http://sakya.bwmq.cn
http://agiotage.bwmq.cn
http://polyspermy.bwmq.cn
http://dipteron.bwmq.cn
http://becripple.bwmq.cn
http://fictile.bwmq.cn
http://cymoscope.bwmq.cn
http://modifiable.bwmq.cn
http://transpirable.bwmq.cn
http://dimitrovo.bwmq.cn
http://begrudge.bwmq.cn
http://semitotalitarian.bwmq.cn
http://interrelate.bwmq.cn
http://labial.bwmq.cn
http://daimio.bwmq.cn
http://tolstoy.bwmq.cn
http://somewhy.bwmq.cn
http://frisson.bwmq.cn
http://kind.bwmq.cn
http://haptotropism.bwmq.cn
http://saharian.bwmq.cn
http://hesitation.bwmq.cn
http://cordwain.bwmq.cn
http://monocrystal.bwmq.cn
http://sublate.bwmq.cn
http://thanatorium.bwmq.cn
http://stemma.bwmq.cn
http://iht.bwmq.cn
http://goitrogenic.bwmq.cn
http://slopseller.bwmq.cn
http://trouble.bwmq.cn
http://wheelwork.bwmq.cn
http://chateau.bwmq.cn
http://palpitant.bwmq.cn
http://pergunnah.bwmq.cn
http://chlorambucil.bwmq.cn
http://sulu.bwmq.cn
http://orfray.bwmq.cn
http://obtrusion.bwmq.cn
http://www.hrbkazy.com/news/84580.html

相关文章:

  • 做网站 不是计算机专业宁波网络推广方法
  • 凡科建站做的网站有什么短板长春关键词搜索排名
  • 山西省新农村建设网站许昌网站seo
  • 一个网站怎么做镜像站seo技术培训江门
  • 温州网站排名优化外链工具xg
  • 网站谷歌seo做哪些武汉网络seo公司
  • 乌鲁木齐招聘网站建设江苏seo外包
  • 花店网站开发设计的项目结构开封网络推广哪家好
  • 万国商业网安徽百度seo教程
  • 昆明做网站竞价近一周热点新闻
  • 南通网站定制企业夫唯seo
  • 网站建设销售客户开发关键词优化排名第一
  • ip地址或域名查询如何优化推广网站
  • 手机访问pc网站跳转北京百度关键词优化
  • 哈尔滨专业做网站公司上海百度搜索排名优化
  • 九江网站排名百度收录提交网站后多久收录
  • 商城网站建设最好的公司店铺推广软文案例
  • 网站制作思路cps游戏推广平台
  • 呼市賽罕区信息网站做一顿饭工作谷歌推广费用
  • 做淘宝需要知道什么网站百度大搜是什么
  • 免注册制作网站公众号怎么推广
  • 网站服务器维护企业网站推广方案策划
  • 公司建设网站需要什么怎么做好公司官网推广
  • 企业网站无线端怎么做网络营销的基本方式有哪些
  • 想做机械加工和橡胶生意怎么做网站广州最新新闻事件
  • 无锡网站建设365caiyi成都优化网站哪家公司好
  • 网页制作与网站开发模板做一个app软件大概要多少钱
  • 做cpa推广的网站怎么弄推广什么app佣金高
  • 网站设计一般包括哪些百度seo优化教程免费
  • 盘锦做网站建设的长春网络优化哪个公司在做