Blowing in the wind(python编程实现) 给以下歌曲(保存在歌词.txt文件中)加上标题(Blowing in the wind) 作者(Bob Dylan) 和 尾注1962 by Warner Bros. Inc. How many roads must a man walk down Before they call him a man How many seas must a white dove sail Before she sleeps in the sand How many times must the cannon balls fly Before they're forever banned The answer my friend is blowing in the wind The answer is blowing in the wind
时间: 2023-06-09 10:06:47 浏览: 267
以下是Python编程实现Blowing in the wind歌曲的标题和尾注添加:
```python
# 打开歌词文件
with open('歌词.txt', 'r') as f:
lyrics = f.read()
# 添加标题和作者
lyrics = 'Blowing in the wind\n作者:Bob Dylan\n\n' + lyrics
# 添加尾注
lyrics += '\n\n1962 by Warner Bros. Inc.'
# 将歌词文件重新写回
with open('歌词.txt', 'w') as f:
f.write(lyrics)
```
执行以上代码后,歌词.txt文件中的内容将变成:
```
Blowing in the wind
作者:Bob Dylan
How many roads must a man walk down
Before they call him a man
How many seas must a white dove sail
Before she sleeps in the sand
How many times must the cannon balls fly
Before they're forever banned
The answer my friend is blowing in the wind
The answer is blowing in the wind
1962 by Warner Bros. Inc.
```
相关问题
Blowing in the wind(python编程实现) 给以下歌曲加上标题(Blowing in the wind) 作者(Bob Dylan) 和 尾注1962 by Warner Bros. Inc. How many roads must a man walk down Before they call him a man How many seas must a white dove sail Before she sleeps in the sand How many times must the cannon balls fly Before they're forever banned The answer my friend is blowing in the wind The answer is blowing in the wind
以下是 Python 代码实现:
```
# 加上标题
title = "Blowing in the wind"
song = "" # 存储歌曲内容
# 建立歌曲内容列表
lines = [
"How many roads must a man walk down",
"Before they call him a man",
"",
"How many seas must a white dove sail",
"Before she sleeps in the sand",
"",
"How many times must the cannon balls fly",
"Before they're forever banned",
"",
"The answer my friend is blowing in the wind",
"The answer is blowing in the wind"
]
# 将歌曲内容转化为字符串
for line in lines:
song += line + "\n"
# 加上作者和尾注
song += "\nby Bob Dylan\n1962 by Warner Bros. Inc."
# 输出完整歌曲
print(title)
print(song)
```
输出:
```
Blowing in the wind
How many roads must a man walk down
Before they call him a man
How many seas must a white dove sail
Before she sleeps in the sand
How many times must the cannon balls fly
Before they're forever banned
The answer my friend is blowing in the wind
The answer is blowing in the wind
by Bob Dylan
1962 by Warner Bros. Inc.
```
Blowing in the wind(编程实现) 给以下歌曲加上标题(Blowing in the wind) 作者(Bob Dylan) 和 尾注1962 by Warner Bros. Inc. How many roads must a man walk down Before they call him a man How many seas must a white dove sail Before she sleeps in the sand How many times must the cannon balls fly Before they're forever banned The answer my friend is blowing in the wind The answer is blowing in the wind
Answer:
以下是我为这首歌加上标题(Blowing in the Wind)和尾注1962 by Warner Bros. Inc.的实现代码,如下所示:
```
def add_title_and_copyright():
song_title = "Blowing in the Wind"
song_author = "Bob Dylan"
year = 1962
copyright = "Warner Bros. Inc."
# Lyrics of the song
lyric1 = "How many roads must a man walk down"
lyric2 = "Before they call him a man"
lyric3 = "How many seas must a white dove sail"
lyric4 = "Before she sleeps in the sand"
lyric5 = "How many times must the cannon balls fly"
lyric6 = "Before they're forever banned"
lyric7 = "The answer my friend is blowing in the wind"
lyric8 = "The answer is blowing in the wind"
# Combine title, author, and year with lyrics
full_lyrics = song_title + "\n" + "Author: " + song_author + "\n" + "Year: " + str(year) + "\n" \
+ lyric1 + "\n" + lyric2 + "\n" + lyric3 + "\n" + lyric4 + "\n" \
+ lyric5 + "\n" + lyric6 + "\n" + lyric7 + "\n" + lyric8 + "\n" \
+ "©" + str(year) + " " + copyright
return full_lyrics
```
调用该函数即可得到加上标题和版权信息的歌曲lyrics,如下所示:
```
print(add_title_and_copyright())
```
输出结果如下:
```
Blowing in the Wind
Author: Bob Dylan
Year: 1962
How many roads must a man walk down
Before they call him a man
How many seas must a white dove sail
Before she sleeps in the sand
How many times must the cannon balls fly
Before they're forever banned
The answer my friend is blowing in the wind
The answer is blowing in the wind
©1962 Warner Bros. Inc.
```
阅读全文