0

该程序几乎总是返回“这是平局(或平局)”。只是我还是有什么问题?这是一个剪刀石头布程序,进行 10 轮并最终显示结果。

#!/usr/bin/python  
# RockPaperScissors from Python
import random;
i = 1;
c = 0;
u = 0;
d = 0;
while i <= 10:
    userAnswer = input("Do you choose rock, paper, or scissors?");
    computerAnswer = random.randint(1, 3);
    if (computerAnswer == 1): 
        computerAnswer = "rock";
    elif (computerAnswer == 2): 
        computerAnswer = "paper";
    else: 
        computerAnswer = "scissors";
    if (computerAnswer == "rock" and userAnswer == "paper"):
       print("You won(paper beats rock)");
       u = u + 1;
    elif (computerAnswer == "" and userAnswer == "paper"):  
        print("You lost(rock beats scissors)");
        c = c + 1;
    elif (computerAnswer == "paper" and userAnswer == "rock"):
        print("You lost(paper beats rock)");
        c = c + 1;
    elif (computerAnswer == "paper" and userAnswer == "scissors"):
        print ("You won(scissors beat paper)");
        u = u + 1;
    elif (computerAnswer == "scissors" and userAnswer == "paper"):
        print("You lost(scissors beats paper)");
        c = c + 1;
    elif (computerAnswer == "scissors" and userAnswer == "rock"):
        print("You won(rock beats scissors)");
        u = u + 1;
    else:
        print("It's a draw!");
        d = d + 1;

    if (i == 10):
        print("You won " + str(u) + " times.");
        print("You lost " + str(c) + " times.")
        print("It was a draw " + str(d) + " times.");
    i += 1;

Python的版本是3.2(Python 3.2)

4

2 回答 2

2

您的一个测试用例有几个错误。

elif (computerAnswer == "" and userAnswer == "paper"):  
    print("You lost(rock beats scissors)");
    c = c + 1;

修复computerAnswer == ""计算机的答案是石头而不是什么,使用户的答案是剪刀而不是纸。

于 2013-05-14T01:14:18.027 回答
0

我更正了第一个“elif”语句,那里的比较是错误的,这会扭曲概率。

然后我尝试了几次(Python 2.7.3),这些案例似乎以正确的比例出现。在这三种情况(玩家 1 获胜、玩家 2 获胜、平局)中,每种情况都有三种可能性,因此您希望在 1/3 的情况下赢得 1/3,输掉 1/3 并在 1/3 的情况下取得平局(但是我没有统计检验...)。

于 2013-05-14T01:24:13.480 回答