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

テスト ケースの 1 つにいくつかのエラーがあります。

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

computerAnswer == ""コンピュータの答えが何もないのではなく岩であり、ユーザーの答えが紙の代わりにハサミになるように修正します。

于 2013-05-14T01:14:18.027 に答える
0

最初の「elif」ステートメントを修正しました。比較が間違っています。これにより、確率が歪められます。

I then just tried it a couple of times (Python 2.7.3) and the cases seem to come up in the correct proportion. Of the three cases (player1 wins, player2 wins, draw) each has three possibilities attached, so you'd expect to win 1/3, lose 1/3 and have a draw at 1/3 of the cases (however I made no statistical test...).

于 2013-05-14T01:24:13.480 に答える