将变量分配给随机选择的名称?

时间:2017-03-27 00:48:52

标签: python

我无法在我的项目中将变量(在本例中为“英雄”)分配给随机选择的名称(Marth,Lucina等)。下面是我的代码的一小部分。 (我必须将它分配给一个变量,以便将这个随机文本(名称)打印到画布中。)

问题:英雄会说不明白

import random

focus_chance = 3
five_star_chance = 3
four_star_chance = 36
three_star_chance = 58


summon = random.randint(1, 100)

if summon < 4:
    #print('YOU GOT A 5-STAR FOCUS :DD')        
    focus_hero = ['Marth', 'Lucina', 'Robin', 'Tiki']
    hero = random.choice(focus_hero)           

    if summon > 3 and summon < 8:
        #print('YOU GOT A 5-STAR :D')
        five_hero = ['Ogma', 'Cain', 'Corrin', 'Chrom', 'Caeda', 'Ryoma',
                     'Lyn', 'Tiki', 'Tharja', 'Lilina', 'Leo', 'Azura', 'Abel'
                     'Effie'] 
        hero = random.choice(five_hero)

        if summon > 7 and summon < 45:
            #print('You got a 4-star. :)')
            four_hero = ['Hero1','Hero2']
            hero = random.choice(four_hero)

            if summon > 44 and summon < 101:
                #print('You got a 3-star. :(')
                three_hero = ['Hero3', 'Hero4']
                hero = random.choice(three_hero)
print hero

1 个答案:

答案 0 :(得分:1)

我相信你的问题是你的if是嵌套的。这意味着一个if只能评估其上方if的条件是否为真。例如,

if summon < 4:
    #Only get here if summon is less than 4

    if summon > 3 and summon < 8:
        #Only get here is summon is less than 4 AND summon is greater than 3 and summon is less than 8

解决这个问题的方法是均匀地缩进if,从而使python分别查看每一个:

if summon < 4:
    #Only get here if summon is less than 4

if summon > 3 and summon < 8:
    #Only get here when summon is greater than 3 and less than 8

希望这有帮助!