我自己创建了这样的Python代码:
import random
# nouns-----------------------------------------------------------------------------------------------------------------
nouns = 'noun1 noun2 noun3 noun4 noun5 noun6 noun7 noun8 noun9 noun10 noun11 noun12 noun13 noun14 noun15 noun16' .split()
def getRandomWord(nounList):
wordIndex = random.randint(0, len(nounList) - 1)
return nounList[wordIndex]
noun_var = getRandomWord(nouns)
# verbs-----------------------------------------------------------------------------------------------------------------
verbs = 'verb1 verb2 verb3 verb4 verb5 verb6 verb7 verb8 verb9 verb10 verb11 verb12 verb13 verb14 verb15 verb16' .split()
def getRandomWord(verbList):
wordIndex = random.randint(0, len(verbList) - 1)
return verbList[wordIndex]
verb_var = getRandomWord(verbs)
# infinitives-----------------------------------------------------------------------------------------------------------
infi = 'verb1 verb2 verb3 verb4 verb5 verb6 verb7 verb8 verb9 verb10 verb11 verb12 verb13 verb14 verb15 verb16' .split()
def getRandomWord(infiList):
wordIndex = random.randint(0, len(infiList) - 1)
return infiList[wordIndex]
infi_var = getRandomWord(infi)
# print strings
for i in range(1):
print (noun_var, verb_var, infi_var)
但每当我想将“第25行,第16行”中的1更改为输入变量,让您能够选择所需的句子数时,它只是一次又一次地重复相同的句子,而不是生成新的那些。有没有其他方法可以使用相同的原则制作具有Python的应用程序,或者有办法解决这个问题吗?
此代码位于Python-3.x
中答案 0 :(得分:1)
如果要在循环的每次迭代中显示不同的句子,则生成一个新句子。现在你正在挑选3个随机单词,然后一遍又一遍地打印它们。
for i in range(int(input())):
print (random.choice(nouns), random.choice(verbs), random.choice(infi))