我是python和编程的新手!
我需要编写一个包含4个元组的程序,每个元素包含5个元素。一个元组应该有动词,一个元组应该有名词,一个元组应该有形容词,一个元组应该有副词。然后我必须使用0到4之间随机生成的数字来从每个元组中选择一个元素。这就是我到目前为止所做的:
import random
nouns = ("puppy", "car", "rabbit", "girl", "monkey")
verbs = ("runs", "hits", "jumps", "drives", "barfs")
adv = ("crazily.", "dutifully.", "foolishly.", "merrily.", "occasionally.")
adj = ("adorable", "clueless", "dirty", "odd", "stupid")
num = random.randrange(0,5)
print (num)
有人能告诉我我做错了吗?
答案 0 :(得分:7)
您可以在列表推导中使用random.choice
,然后将所选列表与join
连接起来:
>>> l=[nouns,verbs,adj,adv]
>>> ' '.join([random.choice(i) for i in l])
'girl runs dirty crazily.'
>>> ' '.join([random.choice(i) for i in l])
'monkey hits clueless occasionally.'
答案 1 :(得分:4)
您理解的最简单方法:
import random
nouns = ("puppy", "car", "rabbit", "girl", "monkey")
verbs = ("runs", "hits", "jumps", "drives", "barfs")
adv = ("crazily.", "dutifully.", "foolishly.", "merrily.", "occasionally.")
adj = ("adorable", "clueless", "dirty", "odd", "stupid")
num = random.randrange(0,5)
print nouns[num] + ' ' + verbs[num] + ' ' + adv[num] + ' ' + adj[num]
答案 2 :(得分:2)
对于类似于已接受答案的函数,该函数返回所有可能的排列:
import random
def make_random_sentence():
nouns = ["puppy", "car", "rabbit", "girl", "monkey"]
verbs = ["runs", "hits", "jumps", "drives", "barfs"]
adv = ["crazily.", "dutifully.", "foolishly.", "merrily.", "occasionally."]
adj = ["adorable", "clueless", "dirty", "odd", "stupid"]
random_entry = lambda x: x[random.randrange(len(x))]
return " ".join([random_entry(nouns), random_entry(verbs), random_entry(adv), random_entry(adj)])
答案 3 :(得分:1)
如果你想为每个tupple选择总是相同的索引,那么尝试这样的事情:
import random
idx = random.randrange(0, 5)
print ("%s %s %s %s"%(nouns[idx], verbs[idx], adv[idx], adj[idx]))
如果您想获得每个tupple的随机元素,以下内容应该有效:
import random
print("%s %s %s %s"%(random.choice(nouns), random.choice(verbs), random.choice(adv), random.choice(adj))
答案 4 :(得分:1)
num = random.randrange(0,5)
print (num)
只需提供您生成的随机数。您需要使用您生成的随机数来访问元组。
print(nouns[num])
print(verbs[num])
print(adv[num])
print(adj[num])
答案 5 :(得分:0)
我会使用random.choice执行以下操作,因为它比random.randrange更好...
words = (adj, nouns, verb, adv)
sentence = "".join(choice(word) for word in words)
答案 6 :(得分:0)
random.choice
是从列表、元组或类似容器中随机选择一个事物。
最后一行的作用是从每个列表中随机选择一个事物,并从选择中创建一个新列表。然后打印该列表。在循环中,无论用户输入什么数字,都会执行 X 次。
from random import choice
nouns = ("puppy", "car", "rabbit", "girl", "monkey")
verbs = ("runs", "hits", "jumps", "drives", "barfs")
adv = ("crazily.", "dutifully.", "foolishly.", "merrily.", "occasionally.")
adj = ("adorable", "clueless", "dirty", "odd", "stupid")
# asking user as to how many sentences he would like to generate
for _ in range (int (input ("Enter integer value :"))):
print(list(map(choice, [nouns, verbs, adv, adj])))
Enter integer value :2
['car', 'barfs', 'dutifully.', 'odd']
['rabbit', 'runs', 'occasionally.', 'clueless']
[Program finished]
答案 7 :(得分:-1)
import random
nouns = ("puppy", "car", "rabbit", "girl", "monkey")
verbs = ("runs", "hits", "jumps", "drives", "barfs")
adv = ("crazily.", "dutifully.", "foolishly.", "merrily.", "occasionally.")
adj = ("adorable", "clueless", "dirty", "odd", "stupid")
num = random.randrange(0,5)
print(nouns[num],verbs[num],adv[num],adj[num])