2012-04-18 4 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.') 

저는 파이썬에 익숙하지 않으므로 분명히 뭔가 빠뜨린 것 같습니다. 프로그램이 정상적으로 실행됩니다. 그것은 바위, 종이 또는 가위 게임입니다. 5 경기가 치러지고 경기가 끝나면 점수가 표시됩니다. 지금은 단지 1 0, 0 0 또는 0 1을 말합니다. 왜 이런지 모르겠습니다. 내 루프에 문제가없는 것처럼 내 들여 쓰기와 관련이 있다고 생각합니다.기본 들여 쓰기 - 파이썬

+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입니다. 따라서 현재 모든 if UserChoice == ... 조건은 while 루프가 완료된 후 한 번만 검사됩니다 (이로 인해 1 0, 0 0 또는 0 1이 표시됨). 내가 제안하는 라인을 식별한다면 그들은 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 == 줄을 들여 써야합니다.