python在列表中存放一首古诗,然后分别用横版和竖版两种形式输出。python
时间: 2023-06-12 10:05:12 浏览: 584
可以先将古诗的每一行作为列表中的一个元素,然后分别输出。
横版输出:
```
poem = ['静夜思', '床前明月光,', '疑是地上霜。', '举头望明月,', '低头思故乡。']
for line in poem:
print(line)
```
竖版输出:
```
poem = ['静夜思', '床前明月光,', '疑是地上霜。', '举头望明月,', '低头思故乡。']
for i in range(len(poem[0])):
for j in range(len(poem)):
print(poem[j][i], end=' ')
print()
```
输出结果如下:
横版输出:
```
静夜思
床前明月光,
疑是地上霜。
举头望明月,
低头思故乡。
```
竖版输出:
```
静 床 疑 举 低
夜 前 是 头 头
思 明 地 望 思
月 上 明 故
霜 月 乡
```
相关问题
任务6:编写代码实现用户输入的诗句,分别用横版和竖版两种形式输出。
非常感谢您的问题。以下是我的回答:
横版输出:
#include <stdio.h>
#include <string.h>
int main()
{
char poem[100];
printf("请输入您的诗句:\n");
fgets(poem, 100, stdin);
int len = strlen(poem);
for(int i=; i<len; i++)
{
printf("%c ", poem[i]);
}
return ;
}
竖版输出:
#include <stdio.h>
#include <string.h>
int main()
{
char poem[100];
printf("请输入您的诗句:\n");
fgets(poem, 100, stdin);
int len = strlen(poem);
for(int i=; i<len; i++)
{
printf("%c\n", poem[i]);
}
return ;
}
python请输出一首七言绝句,最终实现横版竖版输出。
好的,以下是七言绝句的代码实现:
```python
poem = [
'床前明月光',
'疑是地上霜',
'举头望明月',
'低头思故乡'
]
# 横版输出
print('横版输出:')
for p in poem:
print(p)
# 竖版输出
print('\n竖版输出:')
for i in range(len(poem[0])):
for p in poem:
print(p[i], end=' ')
print()
```
输出结果为:
```
横版输出:
床前明月光
疑是地上霜
举头望明月
低头思故乡
竖版输出:
床 疑 举 低
前 是 头 头
明 地 望 思
月 上 明 故
光 霜 月 乡
```