简体   繁体   English

如何在Python中选择与用户输入的随机交互?

[英]How to choose a random interaction with user input in Python?

I want to be able to randomly choose a joke from a list of jokes, and continue on through the joke (whether it be knock knock, or whatever else) with user input. 我希望能够从一个笑话列表中随机选择一个笑话,并通过用户输入继续讲笑话(无论是敲敲还是其他什么)。

I know what I need to do to have one simple knock knock joke with user interaction, but I want to be able to do several different ones at random. 我知道我需要做一个与用户交互的简单敲打笑话,但是我希望能够随机做几个不同的敲打笑话。

So in terms of pseudocode, I'd like it to look something like this: 因此,就伪代码而言,我希望它看起来像这样:

print("Would you like to hear a joke?")
answer = input()
if answer == ("Yes"):
   choose joke from list of jokes ["joke a", "joke b", "joke c"]
   print("randomly chosen joke")
   continue on with user input
else:
   sys.exit()

Randomly selecting an element from a list can be done like this 可以像这样从列表中随机选择一个元素

import random
joke_list = ['joke1', 'joke2', 'joke3']
random.choice(joke_list)

But as it is, this just selects a string. 但实际上,这只是选择一个字符串。 What you want is something that chooses an interaction . 您想要的是选择交互的东西。 That can be done with something like this 可以用这样的东西来完成

def joke1():
    #Execute joke 1
    pass

def joke2():
    #Execute joke 1
    pass

joke_list = [joke1, joke2] #list of functions
import random
joke = random.choice(joke_list)
joke() #execute the selected joke, which is a function     

So to summarize: make your jokes functions instead of strings so they can each be a unique interaction, make a list of functions, select an random element with random.choice 总结一下:使笑话函数代替字符串,使它们每个都可以是唯一的交互,列出函数,选择带有random.choice的随机元素

joke = random.choice(["joke a", "joke b", "joke c"])

Other answers and comments have suggested using random.choice , but I think that's actually the wrong thing to use in this case, since it may repeat the same joke more than once within the same session. 其他答案和评论建议使用random.choice ,但是在这种情况下,我认为这实际上是错误的做法,因为它可能在同一会话中多次重复同一笑话。 I suspect that would be a lousy experience for the user, so here's an alternative. 我怀疑这对于用户而言将是糟糕的体验,因此这是另一种选择。

Use random.shuffle to randomly order a list, then iterate over it to get your jokes one by one until you either run out or the user doesn't want any more: 使用random.shuffle随机排列一个列表,然后对其进行迭代以获得一个笑话,直到您用完了或者用户不再想要它:

import random

jokes = [x, y, z]     # these could be strings, or functions as suggested by GraphicsNoob

random.shuffle(jokes) # put the list in a random order

it = iter(jokes)      # an iterator over the shuffled list

first = next(it)
print(first)          # tell the first joke, could be first() instead

for joke in it:       # loop over the rest of the jokes
    response = input("Would you like to here another joke?"):   # ask about more
    if response.lower().startswith("n"):                   # stop if the user says "no"
        break
    print(joke)       # tell the next joke, could be joke() if you're using functions

Here's how you do it: 这是您的操作方式:

import random

x = ['foo', 'bar', 'baz']
print x[random.randint(0, len(x)-1)]

Generate a random integer between 0 and however long your joke array is; 生成一个介于0到您的笑话数组有多长之间的随机整数; print that array element. 打印该数组元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM