多次使用相同的随机生成数
Use the same Random Generated Number Several Times
我正在编写一个代码,让用户输入一个句子,一次输入一个单词。当他们键入 'exit' 时,代码 returns "Your original sentence was sentence. Word number (generate random number) is (the word that corresponds to that number)".
例如,句子是"This is a cool code",它会return“你原来的句子是This is a cool code. word number 3 is a.
现在,我的代码得到两个不同的随机数和单词,所以它会像 "Word number 2 is this" 之类的。我应该如何解决这个问题并让它正常工作?
print ('Think of a sentence')
print ('Type the sentence one word at a time pressing enter after each word')
print ("When you have finished the sentence enter 'exit'")
print ('')
sentence = []
while True:
word = input('')
print ('Accepted', word)
if word == 'exit':
print ('')
print ('Your original sentence was')
outputString = " ".join(sentence)
print (outputString)
wordCount = len(outputString.split())
pleaseWork = (random.randint(0,wordCount))
print('Word number ',pleaseWork,' is ', (sentence[pleaseWork]))
break
sentence.append(word)
您正在使用句子中的字符数为句子中的单词生成索引。将 wordCount = int(len(outputString))
更改为 wordCount = len(sentence) - 1
以获得适当的索引。
它不会得到两个不同的随机数或单词,因为您只调用了一次 randint
。问题是两件事:
wordCount
是当前字符串中的字符数。请改用 len(sentence)
。
randint
包括它的上限(不像 range
排除它这样你就不必考虑这样的事情)意味着 random.randint(0,wordCount)
有时 returns wordCount
等 pleaseWork == wordCount == len(sentence)
(假设您执行上述操作)是一种可能性,并且 sentence[len(sentence)]
必然会引发错误。使用 random.randint(0,wordCount-1)
.
你快到了!
当你这样做时:
pleaseWork = (random.randint(0,wordCount))
你得到一个介于零和 len(outputString)
之间的数字(在 pleaseWork
变量中),它(因为 outputString
是一个字符串)会给你outputString
.
试试看:
>>> len("Hello, my name is BorrajaX")
26
但是,您真正想要的是 0
和 sentence
列表中的项目数之间的随机索引,对吗?因为当您这样做时:sentence[pleaseWork]
您正在使用 pleaseWork
作为 sentence
列表的索引,而不是 outputString
字符串的索引。
所以,你在这里做什么:
wordCount = int(len(outputString))
pleaseWork = (random.randint(0,wordCount))
print('Word number ',pleaseWork,' is ', (sentence[pleaseWork]))
可以缩短为:
pleaseWork = random.randint(0, len(outputString))
print('Word number ',pleaseWork,' is ', (sentence[pleaseWork]))
你现在看到了吗? pleaseWork
包含一个介于 0
和 outputString
之间的数字。 但是 那么您正在使用该号码访问您的列表 sentence
。如果 outputString
有 100 个字符而 sentence
只有三个项目会怎样?好吧......你很可能在那个 random.randint
调用中获得一个大于 2
的整数。假设您得到... 50
...当您尝试访问包含三个项目的列表的第 50 项时会发生什么?
>>> [1, 2, 3][50]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
所以,您应该做的是将 randint
的范围更改为 sentence
列表中的项目数:
wordCount = int(len(sentence))
pleaseWork = (random.randint(0, wordCount))
完整示例:
def sentenceSplit():
print ('Think of a sentence')
print ('Type the sentence one word at a time pressing enter after each word')
print ("When you have finished the sentence enter 'exit'")
print ('')
sentence = []
while True:
word = raw_input('')
print ('Accepted', word)
if word == 'exit':
print ('')
print ('Your original sentence was')
outputString = " ".join(sentence)
print (outputString)
wordCount = int(len(sentence))
pleaseWork = (random.randint(0, wordCount))
print('Word number ', pleaseWork, ' is ', (sentence[pleaseWork]))
break
sentence.append(word)
我正在编写一个代码,让用户输入一个句子,一次输入一个单词。当他们键入 'exit' 时,代码 returns "Your original sentence was sentence. Word number (generate random number) is (the word that corresponds to that number)".
例如,句子是"This is a cool code",它会return“你原来的句子是This is a cool code. word number 3 is a.
现在,我的代码得到两个不同的随机数和单词,所以它会像 "Word number 2 is this" 之类的。我应该如何解决这个问题并让它正常工作?
print ('Think of a sentence')
print ('Type the sentence one word at a time pressing enter after each word')
print ("When you have finished the sentence enter 'exit'")
print ('')
sentence = []
while True:
word = input('')
print ('Accepted', word)
if word == 'exit':
print ('')
print ('Your original sentence was')
outputString = " ".join(sentence)
print (outputString)
wordCount = len(outputString.split())
pleaseWork = (random.randint(0,wordCount))
print('Word number ',pleaseWork,' is ', (sentence[pleaseWork]))
break
sentence.append(word)
您正在使用句子中的字符数为句子中的单词生成索引。将 wordCount = int(len(outputString))
更改为 wordCount = len(sentence) - 1
以获得适当的索引。
它不会得到两个不同的随机数或单词,因为您只调用了一次 randint
。问题是两件事:
wordCount
是当前字符串中的字符数。请改用len(sentence)
。randint
包括它的上限(不像range
排除它这样你就不必考虑这样的事情)意味着random.randint(0,wordCount)
有时 returnswordCount
等pleaseWork == wordCount == len(sentence)
(假设您执行上述操作)是一种可能性,并且sentence[len(sentence)]
必然会引发错误。使用random.randint(0,wordCount-1)
.
你快到了!
当你这样做时:
pleaseWork = (random.randint(0,wordCount))
你得到一个介于零和 len(outputString)
之间的数字(在 pleaseWork
变量中),它(因为 outputString
是一个字符串)会给你outputString
.
试试看:
>>> len("Hello, my name is BorrajaX")
26
但是,您真正想要的是 0
和 sentence
列表中的项目数之间的随机索引,对吗?因为当您这样做时:sentence[pleaseWork]
您正在使用 pleaseWork
作为 sentence
列表的索引,而不是 outputString
字符串的索引。
所以,你在这里做什么:
wordCount = int(len(outputString))
pleaseWork = (random.randint(0,wordCount))
print('Word number ',pleaseWork,' is ', (sentence[pleaseWork]))
可以缩短为:
pleaseWork = random.randint(0, len(outputString))
print('Word number ',pleaseWork,' is ', (sentence[pleaseWork]))
你现在看到了吗? pleaseWork
包含一个介于 0
和 outputString
之间的数字。 但是 那么您正在使用该号码访问您的列表 sentence
。如果 outputString
有 100 个字符而 sentence
只有三个项目会怎样?好吧......你很可能在那个 random.randint
调用中获得一个大于 2
的整数。假设您得到... 50
...当您尝试访问包含三个项目的列表的第 50 项时会发生什么?
>>> [1, 2, 3][50]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
所以,您应该做的是将 randint
的范围更改为 sentence
列表中的项目数:
wordCount = int(len(sentence))
pleaseWork = (random.randint(0, wordCount))
完整示例:
def sentenceSplit():
print ('Think of a sentence')
print ('Type the sentence one word at a time pressing enter after each word')
print ("When you have finished the sentence enter 'exit'")
print ('')
sentence = []
while True:
word = raw_input('')
print ('Accepted', word)
if word == 'exit':
print ('')
print ('Your original sentence was')
outputString = " ".join(sentence)
print (outputString)
wordCount = int(len(sentence))
pleaseWork = (random.randint(0, wordCount))
print('Word number ', pleaseWork, ' is ', (sentence[pleaseWork]))
break
sentence.append(word)