为什么这种摇滚剪刀几乎总是回归"领带(画)"?

时间:2013-05-14 01:05:16

标签: python

这个程序几乎总是返回“这是一个平局(或平局)”。这只是我还是错了? 这是一个Rock Paper Scissors计划,可以完成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)

2 个答案:

答案 0 :(得分:2)

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

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

修复computerAnswer == "",以便计算机的答案是摇滚而不是什么,并使用户的答案剪刀而不是纸张。

答案 1 :(得分:0)

我纠正了第一个“elif”语句,比较错误,这会扭曲概率。

然后我尝试了几次(Python 2.7.3)并且案例似乎以正确的比例出现。在三种情况下(玩家1获胜,玩家2获胜,平局)每个都有三种可能性附加,所以你期望赢得1/3,输掉1/3并且在1/3的情况下获得平局(但是我没有统计测试......)。