python - 为什么我的 python 函数不返回我的任何条件,除了无?

标签 python list python-3.x range

我正在制作一个简单的 RPG 战斗系统,使用此功能来决定玩家将使用什么类型的攻击。但是我没有运气让我的条件返回他们指定的字符串。它只返回“无”一词。

顺便说一句,我大约一个月前才开始学习编程,所以我对 Python 不是很了解。

def attack_or_critical_attack_or_missed():
    #Random number generated from a range and then placed into a list.
    import random
    number_list = [random.randrange(1, 105)]
    print(number_list)
    print("")
    #Check list to see where my generated number gets put. 
    #Big Chance For Regular Attack
    range1 = []
    n = range(1, 76)
    for n in n:
        range1.append(n)
    if range1 in number_list:
        return "normal_attack"
    #Small Chance For A Critical Attack
    range2 = []
    j = range(76, 101)
    for j in j:
        range2.append(j)
    if range2 in number_list:
        return "critical_attack"
    #Tiny Chance For The Attack To Miss
    range3 = []
    v = range(101, 106)
    for v in v:
        range3.append(v)
    if range3 in number_list:
        return "missed_attack"

print(attack_or_critical_attack_or_missed())

最佳答案

试试这个简化版的代码:

def attack_or_critical_attack_or_missed():
    import random
    roll = random.randrange(1, 105)
    if roll in range(1, 76):
        return "normal_attack"
    elif roll in range(76, 101):
        return "critical_attack"
    elif roll in range(101, 106):
        return "missed_attack"
    else:
        raise RuntimeError('This code should not be reached')

print(attack_or_critical_attack_or_missed())

它可能会帮助您了解自己做错了什么。请注意,这是对您的原始代码的简单重构,除了异常之外,我并没有真正添加任何新内容,但我重新安排了您的代码布局方式,以便您更容易理解。

这里有一个更稳健的方法:

import random

def perform_attack(rand_max=105, normal=range(1,76), critical=range(76,101), missed=range(101,106)):
    roll = random.randrange(1, rand_max)
    outcomes = {'normal': normal, 'critical': critical, 'missed': missed}
    for outcome, field in outcomes.items():
        if roll in field:
            return outcome + '_attack'
    raise RuntimeError('Roll was outside of any specified range, check parameters')

这里有一个可以非常动态调整的万无一失的方法:

import random

def attack(**attack_table):
    attack_table.setdefault('normal', 75)
    attack_table.setdefault('critical', 25)
    attack_table.setdefault('miss', 5)
    max_roll = 0
    outcomes = {}
    for outcome, chance in attack_table.items():
        outcomes[outcome] = range(max_roll, max_roll + chance)
        max_roll += chance
    roll = random.randrange(max_roll)
    for outcome, threat_range in outcomes.items():
        if roll in threat_range:
            return outcome

用法:

该功能会根据您分配的机会编号自动为每种类型的攻击建立威胁范围,默认情况下有正常、严重和未命中,所有机会都与您为它们设置的机会相同。如果我要指定一种新的结果类型,比如“ super ”,概率为 500,那么它很有可能成为结果:

>>> attack(super=500)
'super'

我什至可以改变失手的几率:

>>> attack(miss=200)
'miss'

或者甚至一起消除失手几率:

>>> attack(miss=0)
'critical'  # lucky shot!

关于python - 为什么我的 python 函数不返回我的任何条件,除了无?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20882224/

相关文章:

python - 使用Python分离模糊图像

python - 在 Cython 中使用 lambda 函数时出错

python - 当我想将整个网络分成两个模型时,为什么 Keras ,"Graph disconnected"中会发生此错误?

ios - 如何使用Picker(分段控件)过滤列表和/或更新变量的值?

python - 将列表分成字符串,而不是字符

python-3.x - Python Selenium 在网站上找不到 href

python - 反转 'edit_post',参数为 '(' ', )' not found. 1 pattern(s) tried: [' edit_post/(?P<post_id>\\d+)/$']

python - 在 matplotlib 中绘制列表/数组的散点图

python - 为什么我的代码中收到 "list index out of range"?

python - 绘制使用 pandas 创建的带有 CSV 文件的数据框。 x 轴刻度标签有问题