Python 随机短语创建者

Python 随机短语创建者,python,python-3.3,Python,Python 3.3,我想创建一个程序,通过创建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) Ran

我想创建一个程序,通过创建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')
等等。。。附言:使用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.

几乎肯定会更好

有多种更简单的方法。首先想到的是有一个短语列表,然后改用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]))

当然可以。使用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))

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

这里,这最初是从my book code(密码的一种形式)实现(book cipher)中获取的 摘自这里:

但我在代码中做了一些改动,使之符合您的描述

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
BOOK=“shakespeare.txt”#您的书/document.txt文件包含单词(可能是维基百科的文章,随便什么)
#此文件中的内容(本例中为shakespeare.txt);将在随机单词/短语进度中获取和使用。
def GetBookContent(书本):
ReadBook=open(BOOK,“r”)#在阅读模式下打开(本例中为shakespeare.txt),进入ReadBook
txtContent_splitted=ReadBook.read();#将内容(已拆分!)从分配到->txtContent_splitted
ReadBook.close()#合上书
Words=txtContent\u已拆分
返回(txtContent\u splited.split())
boktxt=GetBookContent(书本)
K
#随机性发生在这里。我们导入随机,分配一个范围
#(以列表的形式!)
#从0到10转换为变量K。
#我们洗牌K(因此,我们得到随机单词索引)
导入随机;K=list(范围(0,10));random.shuffle(K);
x=0;klist=K
对于klist中的键:打印(boktxt[int(klist[x]));x=x+1
#试运行:
#生成10个随机单词。
#以前
#说吧。
#我
#听
#任何
#什么
#进行
#此外,
#首先
#我们

这里的第二个是我要建议的。当扩大词表时,几个小时的欢乐接踵而至。例如:沮丧的自行车以詹姆斯·弗兰科的方式产生了民主,而湿海狸为沃尔特·怀特消灭了儿童。
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