我可以在python中为random.randint()函数设置条件

时间:2016-12-19 14:36:57

标签: python list random

WORDS=("lorem","ipsum","python")
words = random.choice(WORDS)
correct = words
length = 0
while length <len(words):
    i = random.randint(0,len(words))
    print(words[i],end=" ")
    length +=1

我正在努力制作一个混乱的游戏,一个单词中的字母应该混搭。我想问一下我是否可以设置一个条件,以便i不会一遍又一遍地重复相同的值

感谢任何帮助。

4 个答案:

答案 0 :(得分:1)

您可以使用就地改组的random.shuffle。例如:

>>> import random
>>> WORDS = ["lorem","ipsum","python"]  # But you need `list` not `tuple`; 
                                        # tuples are immutable
>>> random.shuffle(WORDS)
>>> WORDS  # List is shuffled
['ipsum', 'python', 'lorem']

为了改变你所拥有的元组中的每个单词,你需要这样做:

shuffled_words= []

for word in WORDS:
    word_list = list(word)  # Typecast `str` to `list`
    random.shuffle(word_list)  # shuffle the list
    shuffled_words.append(''.join(word_list)) # join the list to make `str`

shuffled_words列表的值保持为:

>>> shuffled_words
['omelr', 'spmiu', 'pynhot']

答案 1 :(得分:1)

使用random.sample(使用random.shuffle需要创建一个列表,因为它可以就地运行):

shuffled = "".join(random.sample(words,len(words)))

BTW:您的代码必然会崩溃,因为它使用的random.randint可能会选择超出范围的len(words)。 您应该选择random.randrange

答案 2 :(得分:0)

我为你写了一个相当简单的课程,然后详细说明了用法

from random import random
from math import floor

class FairDice():
    def __init__(self, sides = 6):
        self.sides = sides
        self.history = []
        self.last = None
    def roll(self):
        number = random()
        roll = int(floor(number * self.sides)) + 1
        return roll

    def statefull_roll(self, remember = None):
        while True:
            roll = self.roll()
            remember = self.sides if remember is None else remember
            if len(self.history) >= remember:
                return None

            if roll not in self.history:
                self.history.append(roll)
                return roll

    def forget_history(self):
        self.history = []  

    def forget_one(self):
        self.history = self.history[1:]

我已经创建了这个FairDice课程。您可以通过调用.roll()函数来定期滚动。或者,您可以通过调用statefull_roll()方法来执行记住滚动内容的卷。如果没有数字则返回None。您也可以调用forget_history()函数来忘记历史记录。

以下是一个使用示例:

dice = FiarDice(25) #creates a 25 sided dice  

for x in range(10):
    print("Regular Roll gives you: ", dice.roll())  

for x in range(25):
    print("Your sort of Roll gives you: ", dice.statefull_roll()) 

print ( dice.statefull_roll()) # Should return none after exhausting all possible characters. 

dice.forget_history() # forgets that any value have been recorded at all  

让我们说在5卷后重复字母是可以的。我添加了none

roll = dice.statefull_roll(remember = 5)  

这将在5次滚动后返回“无”,之后您只需要做...

if roll is None:
    dice.forget_one()  
    roll = dice.statefull_roll(remember = 5)  

forget_one会忘记你最老的卷。

我所写的内容并不多,但有效且足够灵活,可以在游戏中使用。

答案 3 :(得分:-1)

不,但你可以循环,直到你得到一个你还没有看到的号码:

import random

WORDS = ("lorem", "ipsum", "python")
words = random.choice(WORDS)
correct = words
length = 0
seen = set()
i = 0
while length < len(words):
    while i in seen:
        i = random.randint(0, len(words)-1)
    seen.add(i)

    print(words[i], end=" ")
    length += 1

但是(正如评论者指出的那样)有一种更有效的方式来改变单词的字符:

import random

WORDS = ("lorem", "ipsum", "python")
word = random.choice(WORDS)

charlist = list(word)
random.shuffle(charlist)
print(" ".join(charlist), end=" ")