我有一种预感,这个问题的答案非常简单,但尽管如此,我还是想不通(事实上,我根本不知道这些语言中的任何一种)。我需要的是一个这样工作的脚本:
- 首先,您键入命令类似!random和1-100范围内的一个数字(该数字表示成功的概率,单位为%)[如下:!random 78]
- 然后,它会根据给定的概率选择你是否成功[例如,使用!random 78,结果为"成功"的概率为78%]
- 然后,它会在一个频道上显示结果("成功"或"失败")
我需要这个用于在线文本RPG会话。也为我糟糕的英语感到抱歉。
代码现在的样子:
__module_name__ = "HexChat Randomiser"
__module_version__ = "0.1"
__module_description__ = "A randomiser for HexChat."
import random
import xchat
def callback(word, world_eol, userdata):
number = word[1]
if random_chance(number):
print ("Success")
else:
print ("Failure")
def random_chance(percent_chance):
return random.randrange(1, 101) > (100 - percent_chance)
xchat.hook_command("random", callback, help="/random <number>")
错误:
Traceback (most recent call last):
File "<string>", line 10, in callback
File "<string>", line 17, in random_chance
TypeError: unsupported operand type(s) for -: 'int' and 'str'
首先,您可能需要了解hexchat的Python或Perl文档。
如果你想在python中继续,我已经写了一个小脚本让你开始:
import random
import xchat
def callback(word, world_eol, userdata):
number = word[1]
if random_chance(number):
print "Success"
else:
print "Failure"
def random_chance(percent_chance):
return random.randrange(1, 101) > (100 - int(percent_chance))
xchat.hook_command("random", callback, help="/random <number>")
你必须让它自己在hexchat中工作。要加载脚本,您必须首先将其保存在某个位置,然后调用load
命令:
负载
加载具有给定文件名的脚本/负载也会起作用。