2012-04-18 28 views
1
#RockPS 
import random 

Choices=['R','P','S'] 
UserScore=0 
CpuScore=0 
Games=0 

while Games<6: 
    UserChoice=input('Rock, paper or scissors? (Type R, P or S respectively)') 
    if UserChoice in Choices: 
     Games+=1 
CpuChoice = random.choice(Choices) 

if UserChoice == 'S' and CpuChoice == 'P': 
    UserScore+=1 
if UserChoice == 'P' and CpuChoice == 'R': 
    UserScore+=1 
if UserChoice == 'R' and CpuChoice == 'S': 
    UserScore+=1 
if UserChoice == 'S' and CpuChoice == 'R': 
    CpuScore+=1 
if UserChoice == 'P' and CpuChoice == 'S': 
    CpuScore+=1 
if UserChoice == 'R' and CpuChoice == 'P': 
    CpuScore+=1 

print(UserScore, CpuScore) 
if UserScore>CpuScore: 
    print('Well done, you won!') 
if UserScore==CpuScore: 
    print('You tied!') 
if UserScore<CpuScore: 
    ('Unlucky, you lost.') 

私はPythonには新しいので、明らかに何かが間違っている可能性があります。プログラムは正常に動作します。ロック、ペーパー、はさみのゲームです。 5試合が行われ、試合の最後にスコアが表示されます。現時点では、それだけで1 0、0 0、または0 1いずれかの方法で、唯一の1つのゲームを数える言います。なぜこのことが分かりませんか?私は私のループに問題がないので、私のインデントでやることだと思います。基本字下げ - Python

+4

[PEP-8](http://www.python.org/dev/peps/pep-0008/)は、ローカル変数の '' lowercase_with_underscores''を推奨することを注意 - 'CapWords''は一般クラスのために予約されています - これらの規則に従って、コードを読みやすくします。 –

答えて

0

はい、あなたのインデントが問題であるように見えるん。あなたが行と同じレベルにライン

CpuChoice = random.choice(Choices) 

、次いでライン

if UserChoice == ... 

をidentをべき

if UserChoice in Choices: 

whileループの本体は同じにするときidentatation戻るを終了レベルはwhileです。 whileループが終了した後ので、現時点では、すべてのあなたのif UserChoice == ...条件は(あなたが1 00 0、または0 1を見ている理由である)、一度チェックされます。あなたは、私が提案するラインをidentをした場合、彼らはwhileループ本体の一部となります。

while Games<6: 
    UserChoice=input('Rock, paper or scissors? (Type R, P or S respectively)') 
    if UserChoice in Choices: 
     Games+=1 

は6回を実行し、コードのこの部分が、ここから下のすべての残りの行::

1

はここで何が起こっているのだループの反復がある後

CpuChoice = random.choice(Choices) 

if UserChoice == 'S' and CpuChoice == 'P': 
    UserScore+=1 
if UserChoice == 'P' and CpuChoice == 'R': 
    UserScore+=1 

は一度だけ実行しますコンプリート。彼らはループ本体の一部であるようにif UserChoice ==ラインのすべてがインデントされなければなりません。