回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>所以伙计们,我有这门课。它是静态类的<code>Treasure</code>生成器,返回<code>Spell</code>类、<code>Weapon</code>类、法力值或健康药水值。
问题是,当I <code>return</code>返回值<code>None</code>时,如果我用<code>print</code>代替<code>return</code>,那么in会像在<code>str</code>函数中一样毫无问题地打印它。如何返回类型,使其不显示任何内容?你知道吗</p>
<pre><code>from random import randint
from weapon_class import Weapon
from spell_class import Spell
class Treasure:
@staticmethod
def generate_random_index(limit):
rand_index = randint(0, int(limit))
return rand_index
@staticmethod
def return_generated_treasure(max_mana, max_health):
# generate num from 0-3
rand_gen_num = Treasure.generate_random_index(3)
options = {
0: Treasure.generate_spell,
1: Treasure.generate_weapon,
2: Treasure.generate_mana_pot,
3: Treasure.generate_health_pot
}
# give arguments for mana and health potion functions only (for now)
if rand_gen_num == 2:
options[rand_gen_num](max_mana)
elif rand_gen_num == 3:
options[rand_gen_num](max_health)
else:
# call other functions, which doesn't need arguments,
# like generate spell and weapon
options[rand_gen_num]()
@staticmethod
def generate_spell():
with open('spell_names.txt', 'r') as f:
database_spell_names = f.read().replace('\n', '').split(',')
lst_len = len(database_spell_names) - 1
# generate number in range 0 - <spell names length>
rand_gen_num = Treasure.generate_random_index(lst_len)
spell_name = database_spell_names[rand_gen_num]
spell_mana_cost = randint(5, 35)
spell_damage = randint(5, 40)
cast_range = randint(1, 3)
# return spell
return Spell(spell_name, spell_damage, spell_mana_cost, cast_range)
@staticmethod
def generate_weapon():
with open('weapon_names.txt', 'r') as f:
database_weapon_names = f.read().replace('\n', '').split(',')
lst_len = len(database_weapon_names) - 1
rand_gen_num = Treasure.generate_random_index(lst_len)
weapon_name = database_weapon_names[rand_gen_num]
weapon_damage = randint(5, 40)
# return weapon
return Weapon(weapon_name, weapon_damage)
@staticmethod
def generate_mana_pot(max_mana):
max_possible_mana_limit = max_mana * 1/2
mana_portion = randint(0, int(max_possible_mana_limit))
return mana_portion
@staticmethod
def generate_health_pot(max_health):
max_possible_health_limit = max_health * 1/3
health_portion = randint(0, int(max_possible_health_limit))
return health_portion
def main():
award = Treasure.return_generated_treasure(100, 100)
print (award)
if __name__ == '__main__':
main()
</code></pre>