【发布时间】:2015-02-24 11:02:18
【问题描述】:
我有一种预感,这个问题的答案非常简单,但我还是想不通(我可能根本不懂这些语言的事实)。 我需要的是一个可以这样工作的脚本:
- 首先,键入 !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'
【问题讨论】:
标签: python perl random irc hexchat