2015-02-01 3 views
-4

나는 간단한 Simon이 게임을 완성했다고 말했습니다. 내 문제는 IDLE 3.4는 내가 무엇인지 알 수 없을 때 문제가 있다고 말합니다. (A 연습과)무엇이 문제입니까?

내 코드 :

#Simple pattern repeating game 
#Randomly chooses a pattern then user has to repeat 

import random 

poss = [int(1), int(2), int(3), int(4)] 
#Sets possible choices 
pattern = [] 
#Sets an array for the pattern to be contained in 
plyr = [] 
#Sets an array for the players pattern to be contained in 

def game(): 
#Makes a function for the game to be played 
    pattern.append(random.choice(poss)) 
    #Adds a new value to the game pattern every time 
    for i in pattern: 
    #Loops through the pattern 
     print(i) 
     #Prints the pattern you need to copy 
     #For testing it sticks but this is to be change 


    for i in pattern : 
    #Loops through the pattern so you can give your value 
     plyr[i] = int(input("Pattern part " + (i + 1) + " is?") 
     #Gets your input of that value in the pattern 

if(plyr == pattern): 
#Checks if your pattern is right 
    game() 
    #Continues the game 

내 문제가 if 문에있다, 그것은 콜론 (:)이 잘못되었다는납니다.

문제가 무엇인지 아는 사람이 있습니까?

+0

게임은 인수에 하나의 값만 추가합니다. 루프의 요점은 무엇입니까? –

+0

@MalikBrahimi 첫 번째 루프는 "사이먼이 말하는 것"입니다. 두 번째 루프는 입력을받습니다. – Ashmoreinc

+0

네, 패턴에 하나의 값만 추가합니다. –

답변

2

당신은 앞 줄에 닫는 괄호가 누락되었습니다 :

plyr[i] = int(input("Pattern part " + (i + 1) + " is?") 
#   1  2     3  3   2? 

int() 전화가 제대로 닫히지 않았습니다. 파이썬은 논리적 라인을 다음 닫는 괄호로 확장 할 수 있기 때문에 다음 라인은 표현식의 일부이며, 콜론에 도달 한 시간 만이 명백한 오류입니다.

그러면 i + 1은 정수를 생성하며 문자열과 합계 할 수 없습니다. 당신은 str() 전화를 추가 할 것 :

plyr[i] = int(input("Pattern part " + str(i + 1) + " is?")) 

또는 사용 문자열 형식 : plyr가 비어

plyr[i] = int(input("Pattern part {} is?".format(i + 1)) 

때문에, 이것이 IndexError을 올릴 것이다; 목록에없는 색인은 변경할 수 없습니다. 당신은 아마 대신 목록에 새로운 요소 추가에 의미 :

player_guess = int(input("Pattern part {} is?".format(i + 1)) 
plyr.append(player_guess) 

이 플레이어가 새로운 변수로 생각한다 (쉽게 읽을 수있는)을하고 plyr 목록에 그 결과를 추가합니다.

+0

'i + 1'은 int이며, 문자열에 추가 할 수 없습니다. –

+0

고마워요, 고마워요, – Ashmoreinc

+0

@MalikBrahimi : 고맙습니다, 고마워요. –

1

코드에 약간의 오류가있어서 닫는 괄호가 누락되었습니다. 나는 당신을 위해 한 가지 버전을 보았습니다 :

import random 

poss = [int(1), int(2), int(3), int(4)] 
# Sets possible choices 
pattern = [] 
# Sets an array for the pattern to be contained in 
plyr = [] 
# Sets an array for the players pattern to be contained in 

def game(): 
    # Makes a function for the game to be played 
    pattern.append(random.choice(poss)) 
    # Adds a new value to the game pattern every time 
    for i in pattern: 
     # Loops through the pattern 
     print(i) 
     # Prints the pattern you need to copy 
     # For testing it sticks but this is to be change 

    for i in pattern: 
     # Loops through the pattern so you can give your value 
     plyr[i] = int(input("Pattern part {} is?".format(i + 1))) 
     # Gets your input of that value in the pattern 

if plyr == pattern: 
    # Checks if your pattern is right 
    game() 
+0

고마워요, 제가 살펴본 후 필요한 변경을했습니다. 추가 질문이 있으므로 원래 질문으로 변경하려고합니다. 곧보십시오. – Ashmoreinc

+0

@Ashmoreinc ​​: 질문을 변경하는 대신 (모든 답을 즉각 무효로 만들기), 새로운 질문을 할 것입니다. 그래도 문제를 단순화하려면 모든 코드를 게시하는 대신 [즉각적인 문제를 보여주는 최소한의 예제] (http://stackoverflow.com/help/mcve)를 제공하십시오. –

+0

@MartijnPieters 지난번에 내가 한 일은 5 일 동안 질문을하지 못하게 막았고 다음 번에는 그 사실을 명심해야 겠네. 지금보십시오, 더 많은 것 – Ashmoreinc

관련 문제