2

我想创建一个程序,该程序将通过创建 10 个从 1 到 20 的随机整数来生成随机短语,并且根据每个变量整数,将生成某个单词或短语。有没有比以下更简单的方法:

#This is a random phrase generator
import random
Rand1 = random.randint (1, 20)
Rand2 = random.randint (1, 20)
Rand3 = random.randint (1, 20)
Rand4 = random.randint (1, 20)
Rand5 = random.randint (1, 20)
Rand6 = random.randint (1, 20)
Rand7 = random.randint (1, 20)
Rand8 = random.randint (1, 20)
Rand9 = random.randint (1, 20)
Rand10 = random.randint (1, 20)
    if Rand1 ==1:
        print ('Squirrel')

等等... PS 使用 Python 3

感谢您提供有用的建议。我是 python 新手,有可以帮助我创建更好代码的人非常有帮助。如果有人在乎,我将它用于与您交谈的程序,并为您提供听笑话和玩几个游戏的机会。祝你今天过得愉快。

PPS 我最终选择了:

import random
words = 'squirrel orca ceiling crayon boot grocery jump' .split()
def getRandomWord(wordList):
    # This function returns a random string from the passed list of strings.
    wordIndex = random.randint(0, len(wordList) - 1)
    return wordList[wordIndex]
potato = getRandomWord(words)
print (potato) # plus of course all the other words... this is just the base.
4

4 回答 4

10

当然。使用 Python 列表和/或字典。如果您从一组中选择:

words = ['Some', 'words', 'any', 'number', 'of', 'them']
choices = [random.choice(words) for _ in range(10)]
print(' '.join(choices))

如果没有,您可以使用嵌套列表:

words = [['Sam', 'Joe'], ['likes', 'hates'], ['apples', 'drugs', 'jogging']]
choices = [random.choice(group) for group in words]
print(' '.join(choices))

这可以扩展到任意数量的组和每个组中的单词。

于 2013-04-19T21:35:16.050 回答
4

有多种更简单的方法。首先想到的是有一个短语列表,然后使用 random.choice 代替(这样你也不必担心添加新单词和调整。

例子:

import random

part1 = ("Cat","Dog","Squirrel")
part2 = ("jumps", "walks", "crawls")
part3 = ("under","over")

print (random.choice(part1) + " " +  random.choice(part2))

(编辑为使用 ('s around print for Python 3)

您还可以使用列表列表来构建整个字符串...

words = (part1, part2, part3)
print (" ".join([random.choice(word) for word in words]))
于 2013-04-19T21:34:39.880 回答
0
import random
Rand1 = random.randint (1, 4)
random1words = ['','Squirrel','Falcon','Pig']

print (random1words[Rand1])

几乎肯定会更好

于 2013-04-19T21:32:33.780 回答
0

在这里,这最初取自我的书代码(一种密码形式)实现(书密码)取自这里:arnold/book cipher with python

但是我在代码中更改了几处以使其符合您的描述。

BOOK="shakespeare.txt" # Your book/document .txt file containing words (maybe, articles from wikipedia, whatever,)
#the content from this file (in this case, shakespeare.txt); will be taken and used in the random-word/phrase progress.


def GetBookContent(BOOK):
    ReadBook = open(BOOK, "r")# Open <BOOK>(shakespeare.txt in this case) in reading mode, into ReadBook
    txtContent_splitted = ReadBook.read();# assign the contents(splitted!) from <BOOK> into -> txtContent_splitted
    ReadBook.close()#Closing the book
    Words=txtContent_splitted

    return( txtContent_splitted.split() )

boktxt = GetBookContent(BOOK)
K
#Randomness happens here. We import random, assigns a range 
#(in a form of a list!) 
#from 0 to 10 into the variable K.
#and we shuffle K, (hence, we get the random word-indexes)

import random; K=list(range(0,10)); random.shuffle(K);

x=0;klist=K
for keys in klist: print(boktxt[int(klist[x])]);x=x+1
    
#Test Run:
#generating 10 random words.
#          Before
#          speak.
#          me
#          hear
#          any
#          what 
#          proceed
#          further,
#          First
#          we
于 2020-10-29T18:14:47.087 回答