计划让elif搞砸了

时间:2017-10-23 14:37:58

标签: python python-2.7

石头剪刀码。应该选择一个随机的,并把它放在一个用户选择的位置上,它有时会起作用,但是其他人在相同的可能性,它应该是相同的,它会改变结果

    import random       
def one_game():
    score_bot = 0
    score_player = 0
    a = ["rock", "paper", "scissors"]
    UI = raw_input('(enter your choice rock, paper, or scissors)')
    print(random.choice(a))
    if (random.choice(a)) == (UI):
        print ('Tie')
        score_bot+1
        score_player+1
    elif 'rock' in (UI) and 'scissors' in (random.choice(a)):
        print ('You win!')
        score_player+1
    elif 'rock' in (UI) and 'paper' in (random.choice(a)):
        print ('You lose!')
        score_bot+1
    elif 'paper' in (UI) and 'scissors' in (random.choice(a)):
        print ('You lose!')
        score_bot+1        
    elif 'paper' in (UI) and 'rock' in (random.choice(a)):
        print ('You win!')   
        score_player+1 
    elif 'scissors' in (UI) and 'rock' in (random.choice(a)):
        print ('You lose!')
        score_bot+1
    elif 'scissors' in (UI) and 'paper' in (random.choice(a)):
        print ('You win!')
        score_player+1
        print (score_player)
        print (score_bot)
        if (score_bot) > (score_player):
            print ('bot wins!')
        elif (score_bot) < (score_player):
            print ('Player wins!')

the issue

1 个答案:

答案 0 :(得分:3)

您永远不会为计算机选择随机选择。相反,您在每个if语句中计算一个新的随机数。

尝试:

computer_choice = random.choice(a)

然后用random.choice(a)

替换每个未来的computer_choice