我正在Python
学习,这样的练习有一个小问题(我正在根据 Summerfield 的书学习 - “Programming in Python3
. A detail guide”)
练习本身被缩短了,但任务可以理解 所以从 4 个已经填写的列表(文章、名词、动词、副词)中使用 5 个循环生成句子random.choice()
。这样一个收到的诗句有5行,每一行都有这样随机选择的句子。
我做了什么
-创建4个列表,变量line=5
(行数)
- 暂时执行while
一个有这种条件的循环line!=5
-在循环本身中,已经有内部循环用于迭代每个列表的值,在帮助下选择其中一个并将其random.choice()
显示在屏幕上。
问题如下:
我在Ubuntu
终端中工作并使用我启动我的迷你应用程序,但它没有启动,但它只是换行到终端的下一行(好像命令已执行 - 见图)
Код:
#!/usr/bin/env python3
import random
import sys
listarticles=["the","a","for","an","this","that","those"]
listsush=["cat","dog","man","woman","duck","girl","boy"]
listglag=["sing","walk","run","drink","eat","sleep","cry"]
listnarech=["loudly","fastly","well","badly","quietly","ugly","blackly"]
line = 5
while line!=5:
for ls1 in listarticles:
ls1 = random.choice(listarticles)
print(ls1)
for ls2 in listsush:
ls2 = random.choice(listsush)
print(ls2)
for ls3 in listglag:
ls3 = random.choice(listglag)
print(ls3)
for ls4 in listnarech:
ls4 = random.choice(listnarech)
print(ls4)
line+=1
您可以通过使用循环制作 5 句话来稍微简化代码:
map
为代表句子一部分的每个列表调用random.choice
一个函数:如果我正确理解了任务,那么您需要这样的东西