2014-11-10 3 views
0

나는 파이썬에 대한 지식을 넓히기 위해 노력하고있는 프로젝트에 도움이 필요하다. 나는 교체 할 문자를 입력하고 두 번째 입력에 입력 된 다른 문자로 바꿉니다.짐작 게임에서 파이썬으로 두 개의 입력으로 두 배열을 비교하는 방법

내가이 작업 한 비트는 내가 할 수없는 다음 비트 다. 두 번째 입력의 값이 두 번째 배열의 값과 일치하는지 확인해야합니다.

list1 = [abc, bdc, eea] #visible to the user 
list2 = [123, 243, 551] #the answers 

input1 = a #what the user whats to replace 
input2 = 1 #what it needs to be replaced with 

가리스트에 입력 한 1 : I는 약간 아래도 만들어? # 이미 수행 완료
은 input2의 list2와 동일한 위치입니다.

+2

난 정말 당신이 무엇을하려고하지 않습니다. 어떤 일이 일어나고 그 결과가 무엇인지에 대한 완전한 모범을 보여줄 수 있습니까? 또한 첫 번째 부분을 알아 낸 경우 게시하여주십시오. 그러면 우리는 무슨 일이 일어나고 있는지 볼 수 있으므로 실제로 일을했음을 실제로 보여줄 수 있습니다. – poke

답변

1

를 사용하여 문자열과 enumerate : 나는 이해하면

list1 = ["abc", "bdc", "eea"] #visible to the user 
list2 = ["123", "243", "551"] #the answers 

input1 = "a" #what the user whats to replace 
input2 = "1" 
for ind, ele in enumerate(list1): 
    one, two = ele.find(input1), list2[ind].find(input2) # get index for both 
    if one != -1 and one == two: # if indexes are the same and ele.find(input1) is not -1 
     print("Correct guess") 
     list1[ind] = ele.replace(input1, input2) # replace letter with number 

print(list1) 
['1bc', 'bdc', 'ee1'] 
+0

내 친구, 멋진 프로그래머입니다. 고마워요. – user3258159

1

, 당신은 encryptation의 어떤 종류의 입력 사용자 문자열을 변환 한 후 문자열의 2 개 목록을 비교하기 위해 노력하고 있습니다. 이 경우 나는 같은 것을 할 것입니다 :

alphabet = 'abcdefghijklmnopqrstuvwxyz' 
conversion_dict = {str(elem): index+1 for index,elem in enumerate(alphabet)} 

def encrypt(string, conversion_dict): 
    value = '' 
    for letter in string: 
     number = conversion_dict.get(letter) 
     if not number: 
      print("Letter {} can't be encoded".format(letter)) 
      return None 
     value += str(number) 
    return value 


list1 = ["abc", "bdc", "eea"] 
list2 = ["123", "243", "551"] 
list1_encrypted = [encrypt(string, conversion_dict) for string in list1] 
print("User input: {}".format(list1)) 
print("Encrypted user input: {}".format(list1_encrypted)) 
print("List1_encrypted is equeal to List2? {0}".format(list1_encrypted == list2)) 

출력은 다음과 같습니다

User input: ['abc', 'bdc', 'eea'] 
Encrypted user input: ['123', '243', '551'] 
List1_encrypted is equeal to List2? True 
관련 문제