random.choice()选择多个随机值

时间:2019-06-05 12:15:56

标签: python python-3.x pygame

我正在用Python开发Rock Paper Scissors游戏。我想让系统每隔一次选择1个随机值。但是random.choice()一次选择3个随机值。请帮我解决这个问题。


    def random_computer_choice():
        """Choose randomly for computer."""

        # lookup random.choice()
        out = ['rock', 'paper', 'scissors']
        return random.choice(out)

    def rock():
        global human_choice, computer_choice
        global HUMAN_SCORE, COMPUTER_SCORE

        human_choice = 'rock'
        computer_choice = random_computer_choice()
        choice_result(human_choice, computer_choice)
    ```
    paper and scissors following the same format

    def choice_result(human_choice, computer_choice):

        global COMPUTER_SCORE
        global HUMAN_SCORE

        if human_choice == 'rock' and computer_choice == 'paper':
            COMPUTER_SCORE = COMPUTER_SCORE + 1
            print('Computer won')
        elif human_choice == 'rock' and computer_choice == 'scissors':
            HUMAN_SCORE = HUMAN_SCORE + 1
            print('You win')
        elif human_choice == 'paper' and computer_choice == 'rock':
            HUMAN_SCORE = HUMAN_SCORE + 1
            print('You win')
        elif human_choice == 'paper' and computer_choice == 'scissors':
            COMPUTER_SCORE = COMPUTER_SCORE + 1
            print('Computer won')
        elif human_choice == 'scissors' and computer_choice == 'rock':
            COMPUTER_SCORE = COMPUTER_SCORE + 1
            print('Computer won')
        elif human_choice == 'scissors' and computer_choice == 'paper':
            HUMAN_SCORE = HUMAN_SCORE + 1
            print('You win')
        elif human_choice == computer_choice:
            print("That was a tie")

1 个答案:

答案 0 :(得分:0)

import random


run = True

list = ["stone", "paper", "scissors"]

b_of = int(input("best out of : ? "))
print ("you win with", b_of, "pnt")

sc_p = 0
sc_c = 0 

while run == True:

    w_num = random.choice(list)

    guess = input("what's your choice")


    if guess == "stone" and w_num == "paper":
            sc_c = sc_c + 1
            print ( "paper, oups, computer wins with", sc_c, "pnt, and you have", sc_p, "pnt")

    elif guess == "stone" and w_num == "stone":
            print ( "stone, tie, computer has:", sc_c, "pnt, and you have", sc_p, "pnt")

    elif guess == "stone" and w_num == "scissors":
            sc_p = sc_p + 1
            print ( "scissors, you win, computer has", sc_c, "pnt, and you have", sc_p, "pnt")

    elif guess == "paper" and w_num == "paper":
            print ( "paper, tie, computer has:", sc_c, "pnt, and you have", sc_p, "pnt")

    elif guess == "paper" and w_num == "stone":
            sc_p = sc_p + 1
            print ( "stone, you win, computer has", sc_c, "pnt, and you have", sc_p, "pnt")

    elif guess == "paper" and w_num == "scissors":
            sc_c = sc_c + 1
            print ( "scissors, oups, computer wins with", sc_c, "pnt, and you have", sc_p, "pnt")

    elif guess == "scissors" and w_num == "paper":
            sc_p = sc_p + 1
            print (" paper, you win, computer has", sc_c, "pnt, and you have", sc_p, "pnt")

    elif guess == "scissors" and w_num == "stone":
            sc_c = sc_c + 1
            print ( "stone, oups, computer wins with", sc_c, "pnt, and you have", sc_p, "pnt")

    elif guess == "scissors" and w_num == "scissors":
            print ( "scissors, tie, computer has:", sc_c, "pnt, and you have", sc_p, "pnt")

    else:
            print ("enter a good value; stone, paper of scissors. computer has", sc_c, "pnt, and you have", sc_p, "pnt")


    if sc_c == b_of:
            print ("computer wins with", sc_c, "pnt!!!!")
            run = False

    elif sc_p == b_of:
            print ("you win with", sc_p, "pnt!!!!")
            run = False