python要学什么英文歌_Python趣味学习——用循环实现英文儿歌编写

本文介绍了Python中的for循环如何用于重复执行任务,通过举例说明如何用for循环打印歌词来实现英文儿歌的编程。通过这个过程,展示了编程与教育的结合,如何使学习变得有趣且实用,同时强调了编程在自动化繁琐工作中的优势。
摘要由CSDN通过智能技术生成

1、奇异的循环

计算机除了它强大的数学计算和数据处理能力之外,还有一个很明显的优点,就是它特别擅长做那些繁琐无聊的工作,并且毫无怨言。这一方面,我们人类显然无法与计算机PK。但是我们人类更高明的地方就在于,我们擅长让计算机来替自己完成那些重复的劳动。在Python中,实现这一目的的方法就是利用“循环”。循环可以让计算机不停地重复执行一段代码,一遍又一遍,直到你叫停为止。

bae7042561bd0e318b32b534ee85d9cd.png

2、For 循环

循坏可以分为几种,有时候你并不知道具体要让一段代码重复执行多少次,而有时候你需要计算机确切地执行固定次数。就好像你今天决定出去跑圈,有时候你并不知道今天具体要跑多少圈,你可能只是随着心跑,中间可能就会因为某个原因停下;有时候你却会很清楚地知道今天跑完几圈就就不跑了。

For循环:就是你已经知道有一段代码需要重复执行多少次。

如果你想编一段程序,你希望它会打印出5遍“You are the best!”,那么你可以运用For循环实现这个功能。代码可以如下:

for counter in range(1,6):

print("You are the best!")

在Python程序中,单词“range”后面有一个圆括号,里面有两个数字,它表示指定范围内的所有数字,这个范围是从第一个数字到比最后一个数字小1的数字。例如,这里程序中的range(1,6)表示数字1、2、3、4、5,但是不包括6。

因此,上面这个例子循环体就重复执行了5次。

运行程序,输出的结果就是:

You are the best!

You are the best!

You are the best!

You are the best!

You are the best!

3、利用for循环编写英文儿歌

如果你熟知一些英文启蒙儿歌,特别是跟数字有关的歌曲中,你会发现很多歌曲的歌词,它不停地重复,除了一些数字变化之外,其它的歌词都是一样的。这样的歌词其实就是一个循环体,我们就可以用python中的for循环语句来对它们进行编程。

这里我们以《Five little monkeys》为例,来看如何实现对英文数字儿歌进行for循环编程。先来看一下这首歌的歌词:

Five5little monkeys jumping on the bed,

One fell off and bumped his head.

Mama called the doctor,and the doctor said,

"No more monkeys jumping on the bed!"

Four4little monkeys jumping on the bed,

One fell off and bumped his head.

Mama called the doctor,and the doctor said,

"No more monkeys jumping on the bed!"

Three3little monkeys jumping on the bed,

One fell off and bumped his head.

Mama called the doctor,and the doctor said,

"No more monkeys jumping on the bed!"

Two2little monkeys jumping on the bed,

One fell off and bumped his head.

Mama called the doctor,and the doctor said,

"No more monkeys jumping on the bed!"

One1little monkeys jumping on the bed,

One fell off and bumped his head.

Mama called the doctor,and the doctor said,

"No more monkeys jumping on the bed!"

不难发现,这首歌一共有5段,每一段除了开头一句的数字有变化之外,其它的歌词都是一模一样的。我们再来看,这里的数字是倒着数的,也就是从5数到1,按照5、4、3、2、1这样countdow的。那么我们这设计程序的时候,就可以设置一个初始变量count_down=5,然后程序每执行一次,这个变量count_down就减1,直到循环结束。

我们可以尝试这样编代码:

count_down=5

for counter in range(1,6):

print(str(count_down)+' little monkeys jumping on the bed,\nOne fell off and bumped his head.\nMama called the doctor,and the doctor said,\nNo more monkeys jumping on the bed!')

count_down=count_down-1

其中符号\n表示换行。

运行这段代码,便会得到如下结果:

5little monkeys jumping on the bed,

One fell off and bumped his head.

Mama called the doctor,and the doctor said,

No more monkeys jumping on the bed!

4little monkeys jumping on the bed,

One fell off and bumped his head.

Mama called the doctor,and the doctor said,

No more monkeys jumping on the bed!

3little monkeys jumping on the bed,

One fell off and bumped his head.

Mama called the doctor,and the doctor said,

No more monkeys jumping on the bed!

2little monkeys jumping on the bed,

One fell off and bumped his head.

Mama called the doctor,and the doctor said,

"No more monkeys jumping on the bed!"

1little monkeys jumping on the bed,

One fell off and bumped his head.

Mama called the doctor,and the doctor said,

No more monkeys jumping on the bed!

我们可以看到,利用for 循环语句可以很快地输出重复的歌词文本。当然,类似的英文数字儿歌都可以通过这样的循环语句来实现,如《ten in the bed》、《five little ducks》,感兴趣的同学可以自己尝试看看哦。

4、小结

学习编程,很多人会觉得是一件比较枯燥的事情,而对于学业压力很重的中小学生来说,更会觉得额外去学编程,会占用其他学科的学习时间。但是我们看到,其实在编程的过程中,结合一些有趣的例子,特别是跟其他学科的知识相结合,可以做到既可以学习到编程的知识,同时还可以调用甚至新学习其他学科的知识,达到一举两得的目的。这样,何乐而不为呢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值