Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何执行唯一的随机函数_Python - Fatal编程技术网

Python 如何执行唯一的随机函数

Python 如何执行唯一的随机函数,python,Python,我想制作一个脚本,输入唯一的随机单词,这是我目前的代码: import pynput import random import time from pynput import keyboard from pynput.keyboard import Key, Controller def hi(): keyboard = Controller() keyboard.type('hi') keyboard.press(Key.enter) time.sleep(rando

我想制作一个脚本,输入唯一的随机单词,这是我目前的代码:

import pynput
import random
import time
from pynput import keyboard
from pynput.keyboard import Key, Controller

def hi():
   keyboard = Controller()
   keyboard.type('hi')
   keyboard.press(Key.enter)
   time.sleep(random.randint(1, 2))

def hello():
   keyboard = Controller()
   keyboard.type('hello')
   keyboard.press(Key.enter)
   time.sleep(random.randint(1, 2))

def world():
   keyboard = Controller()
   keyboard.type('world')
   keyboard.press(Key.enter)
   time.sleep(random.randint(1, 2))
每次执行独特的功能时,我应该做些什么

所需的输出示例:


你可以为此做一个函数。以下是方法:

但首先要列出包含所有函数名的列表

functions = [hi, hello, world]

def unique_word(func_list):
    function = random.choice(func_list)
    function()

unique_word(functions)

此函数每次调用时都会调用随机函数

但是,如果您想为三个函数输出生成一个随机顺序,请使用此命令

functions = [hi, hello, world]

random.shuffle(functions)

for function in functions:
    function()


您可以使用
random.choice()
。和
循环以重复该功能

因此,将此添加到您的代码中:

functions = [hi, hello, world]
donefunc = []
def unique_word(func_list):
    global donefunc
    function = random.choice(func_list)
    while function in donefunc:
        function = random.choice(func_list)
    function()
    donefunc.append(function)
for i in range(len(functions)):
    unique_word(functions)
整个代码:

import pynput
import random
import time
from pynput import keyboard
from pynput.keyboard import Key, Controller

def hi():
   keyboard = Controller()
   keyboard.type('hi')
   keyboard.press(Key.enter)
   time.sleep(random.randint(1, 2))

def hello():
   keyboard = Controller()
   keyboard.type('hello')
   keyboard.press(Key.enter)
   time.sleep(random.randint(1, 2))

def world():
   keyboard = Controller()
   keyboard.type('world')
   keyboard.press(Key.enter)
   time.sleep(random.randint(1, 2))

functions = [hi, hello, world]
donefunc = []
def unique_word(func_list):
    global donefunc
    function = random.choice(func_list)
    while function in donefunc:
        function = random.choice(func_list)
    function()
    donefunc.append(function)

for i in range(len(functions)):
    unique_word(functions)

根据Tomerikoo的评论(集):


您只需将函数放入列表中,然后:

随机导入
funcs=[嗨,你好,世界]
随机洗牌(funcs)
对于func中的func:
func()
如果您不想原地移动,可以使用创建新的函数列表:

funcs=[hi,hello,world]
对于random.sample(funcs,k=len(funcs))中的func:
func()

random.choice(函数列表)(
是的,你也可以这么做。但我喜欢使用变量,以防我想添加更多功能。@ppwater是的,这只是一次执行,如果希望得到预期的输出,应将函数调用括在
for
循环中。@Tomerikoo唯一的部分是,随机选择的每个函数都是唯一的,并且不是重复的函数。@Tomerikoo我正在为两个不同的结果编辑代码。一个用于从唯一函数列表中选择,另一个用于生成唯一文本。检查编辑的代码。这是目前为止最好的一个,但我不希望它重复相同的功能twice@Rayr哦,好的,修复了。非常感谢,它工作得很好,但是你能解释一下代码的最后一部分吗?(在“整个代码”部分中)@Rayr最后一部分是什么,
附加
部分还是
for
-循环?还是其他部分?@ppwater
unique\u单词
部分
import pynput
import random
import time
from pynput import keyboard
from pynput.keyboard import Key, Controller

def hi():
   keyboard = Controller()
   keyboard.type('hi')
   keyboard.press(Key.enter)
   time.sleep(random.randint(1, 2))

def hello():
   keyboard = Controller()
   keyboard.type('hello')
   keyboard.press(Key.enter)
   time.sleep(random.randint(1, 2))

def world():
   keyboard = Controller()
   keyboard.type('world')
   keyboard.press(Key.enter)
   time.sleep(random.randint(1, 2))

functions = [hi, hello, world]
donefunc = []
def unique_word(func_list):
    global donefunc
    function = random.choice(func_list)
    while function in donefunc:
        function = random.choice(func_list)
    function()
    donefunc.append(function)

for i in range(len(functions)):
    unique_word(functions)

import pynput
import random
import time
from pynput import keyboard
from pynput.keyboard import Key, Controller

def hi():
   keyboard = Controller()
   keyboard.type('hi')
   keyboard.press(Key.enter)
   time.sleep(random.randint(1, 2))

def hello():
   keyboard = Controller()
   keyboard.type('hello')
   keyboard.press(Key.enter)
   time.sleep(random.randint(1, 2))

def world():
   keyboard = Controller()
   keyboard.type('world')
   keyboard.press(Key.enter)
   time.sleep(random.randint(1, 2))

functions = [hi, hello, world]
donefunc = set()
def unique_word(func_list):
    global donefunc
    function = random.choice(func_list)
    while function in donefunc:
        function = random.choice(func_list)
    function()
    donefunc.add(function) 

for i in range(len(functions)):
    unique_word(functions)