2014-09-21 101 views
-1

我的程序的一部分出现了问题,它检查用户输入的线索是否正确并且解决了在外部文件中解析的版本。试图用另一个列表检查一个列表

我所运行的代码没有错误,但它一直说我有0对配对。这是错误的,因为我已经在线索列表中进行了正确的配对,以检查它是否还在做同样的事情。

我到目前为止的代码就像显示。

def check_clues(): 
    count = 0 
    with open('solved.txt') as r: 
     solved = r.readlines() 
    with open('clues.txt') as r: 
     pairings = r.readlines() 

    for user in pairings: 
     if user in solved: 
      count += 1 

    print('You got:', count, 'correct!') 

Solved.txt文件...

ACQUIRED 
ALMANAC 
INSULT 
JOKE 
HYMN 
GAZELLE 
AMAZON 
EYEBROWS 
AFFIX 
VELLUM 

Clues.txt

A# 
M* 
N% 

码的字,其必须解决...

#+/084&" 
#3*#%#+ 
8%203: 
,1$& 
!-*% 
.#7&33& 
#*#71% 
&-&641'2 
#))85 
9&330* 
+0

有你在什么'pairings'检查,并分别'solved'? – tttthomasssss 2014-09-21 21:48:11

+0

你是什么意思? – Paul 2014-09-21 21:50:55

+2

检查'count + = 1' – koxt 2014-09-21 21:52:05

回答

0

你有= +而不是+ =,缩进有点扭曲,所以我没有确定它如何运行没有错误,但我认为这应该工作。还要检查文件的格式。如果它们是逗号分隔的,则readlines会产生类似[[clue1, clue2, clue3], [clue4, clue5, clue6].... etc]而不是[clue1,clue2, clue3, clue4, ...etc]的内容。

我认为这应该工作:

def check_clues(): 
    count = 0 
    # TRIES TO OPEN FILE SOLVED.TXT 
    try: 
     with open('solved.txt') as r: 
    # READS THROUGH ALL THE LINES IN SOLVED.TXT 
      solved = r.readlines() 
    # WILL DISPLAY AN ERROR MESSAGE IF SOLVED.TXT IS NOT FOUND 
    except: 
     print("Error finding file") 
    # WILL TRY AND OPEN THE FILE 'clues.txt' 
    try: 
     with open('clues.txt') as r: 
      clues = r.readlines() 
    except: 
     print("Error finding ") 


    # GOES THROUGH BOTH THE USERS CLUES AND SOLVED.TXT TO SEE HOW MANY CLUES ARE THE SAME 
    for clue in clues: 
     if clue in solved: 
      count += 1 
    # Prints the amount of clues the user got right out of 10 
    print('You got:', count, 'correct!') 
+0

感谢您的发现!然而,它仍然和以前一样... – Paul 2014-09-21 21:58:28

+0

线索文件格式化如何? – BWStearns 2014-09-21 22:01:20

+0

那是什么? – Paul 2014-09-21 22:01:59

相关问题