【问题标题】:random.choice() chooses more than one random valuerandom.choice() 选择多个随机值
【发布时间】:2019-06-05 12:15:56
【问题描述】:

我正在用 python 开发剪刀石头布游戏。我希望系统每隔一次选择 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")

【问题讨论】:

  • 它不能返回 3 个值,可能发生了其他事情。这就是你所有的代码吗?
  • choice_result(human_choice, computer_choice) 做了什么?
  • 您应该发布您遇到的确切错误。您还应该发布choice_result() 函数。
  • 请解释一下您是如何想到choice 返回多个值的?
  • 当我点击假设摇滚按钮时,它会为相同的输入返回 3 个值

标签: python python-3.x pygame


【解决方案1】:
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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-19
    • 2010-11-24
    • 2019-07-30
    • 2020-05-27
    • 2018-06-13
    • 1970-01-01
    • 2023-01-04
    相关资源
    最近更新 更多