2014-05-03 42 views
0

原谅我,如果这出来有点散漫,当我说我已经在这个程序超过13个小时,现在我严重失眠,我没有夸大。这是我的第四次修订,我真的不知道该怎么做,所以如果任何人都可以帮助我,将不胜感激。我对编程老师的介绍要求我们从他的模板中制作一个“闪存卡”学习程序。我在Windows 7机器上使用Idle 3.3.3。Python程序不会退出;导入随机引起的错误

#Flash Cards 
#Uses parallel arrays to store flash card data read from file 
#Quizzes user by displaying fact and asking them to give answer 
import random 
def main(): 
    answer = []    #array to store answer for each card 
    fact = []    #array to store fact/definition for each card 
    totalTried = 0   #stores number of cards attempted 
    totalRight = 0   #stores number of correct guesses 
    loadCards(answer, fact) #call loadcards() and pass it both arrays 
    numCards = len(answer) #find number of cards loaded 
    keepGoing = "y" 

    while keepGoing == "y" or keepGoing == "Y": 
     #Enter your code below this line 

     # 2a. Pick random integer between 0 and numCards and store the 
     #  number in a variable named randomPick. 
     randomPick = random.randint (0, numCards) 
     # 2b. Add one to the totalTried accumulator variable. 
     totalTried = totalTried + 1   
     # 2c. Print element randomPick of the fact array. This shows the 
     #  user the fact/definition for this flashcard. 
     print (fact [randomPick]) 
     # 2d. Prompt the user to input their guess and store the string they 
     # enter in a variable named "userAnswer" 
     userAnswer = input ('What is your answer?') 
     # 2e. Compare the user's guess -userAnswer- to element 
     #  -randomPick- of the answer array. 
     if userAnswer == (answer [randomPick]): 
      # 2e-1 If the two strings are equal, tell the user they 
      # guessed correctly and add 1 to the totalRight 
      # accumulator variable. 
      print ('That is correct.') 
      totalRight == totalRight + 1 
     # 2e2. If the two strings are not equal, tell the user they guessed 
     # wrong and display the correct answer from the answer array. 
     else: 
      print ('That is incorrect.') 
      print (answer [randomPick]) 
     #2f. Prompt the user the user to see if they want to continue and 
     #store their response in the keepGoing variable. 
     keepGoing = input ('Would you like to continue?') 

     #Enter your code above this line 

    print("You got", totalRight, "right out of", totalTried, "attempted.") 

def loadCards(answer, fact): 
    #Enter your code below this line 
    # 1a. Open flashcards.txt in read mode & assign it var name "infile"  
    infile = open('flashcards.txt', 'r') 
    # 1b. Read 1st line from file and store in var. name "line1" 
    line1 = infile.readline() 
    # 1c. Use while loop to make sure EoF has not been reached. 
    while line1 != '': 
     # 1c1. Strip newline escape sequence (\n)from variable's value. 
     line1 = line1.rstrip ('\n') 
     # 1c2. Append string to answer array. 
     answer.append (line1) 
     # 1c3. Read next line from file and store in var. name "line2" 
     line2 = infile.readline() 
     # 1c4. Strip newline escape sequence (\n) from variable's value. 
     line2 = line2.rstrip ('\n') 
     # 1c5. Append the string to the fact array. 
     fact.append (line2) 
     # 1c6. Read next line from file and store it in var. name "line3". 
     line3 = infile.readline() 
     # 1d. Close file. 
    infile.close() 


    #Enter your code above this line 


main() 

当我运行程序没有实际发生,但是当我尝试之后关闭shell窗口,它告诉我,方案仍在运行并问我是否要杀死它。

当我尝试检查它时,调试器也没有显示任何信息。但是,如果我将代码复制到shell并从那里运行,则会得到“SyntaxError:编译单个语句时发现的多个语句”。这两个文件都没有改变,但早些时候它告诉我“随机导入”存在问题。

在此先感谢您的帮助。

+0

它也有助于包含实际的回溯,不仅仅是例外,或者更糟糕的是,“出现了问题”。回溯有重要的信息,包括指出错误发生的位置和位置,这使得它更容易帮助你。 –

+0

请更改您的问题,提供错误追溯,相关代码,预期输入/输出。你必须帮助我们帮助你。 – sshashank124

+0

@Wooble:有效点。正如我所说,我没有睡太多。我回来编辑我的帖子,但其他人已经有了。 – user3598983

回答

2

我快速看了一下,大部分对我来说似乎都没问题。我改变了()输入到的raw_input()(其中两个在你的代码),发现你有一个双等号时,你可能是指一个单一的一个

线36:

totalRight == totalRight + 1 

改为

totalRight = totalRight + 1 

它修正你的正确答案计数器和线68:

line3 = infile.readline() 

变更为

line1 = infile.readline() 

否则它会在您的while循环中永远被捕获。而我只是复制线54:

line1 = infile.readline() 

并粘贴它,所以它是有两次添加其他的ReadLine()调用,只需跳过你的文本文件的第一行的懒办法,因为它似乎是一个评论,而不是答案和问题的一部分。你可能不想这样做,只是从你的文本文件中删除评论。 = b

随着这些变化,它似乎为我工作得很好。

+0

非常感谢。当我尝试“raw_input”而不是“输入”时,它仍然带着错误回来,但是你指出的其他缺陷阻止了它的工作。 – user3598983

1

由于这是一类(我不仅可以发表评论,我只能回答)我想补充一点,其实是这样的东西too many comments

这些意见(和说实话,最您的意见),分心和不必要的

answer = []    #array to store answer for each card 
fact = []    #array to store fact/definition for each card 
totalTried = 0   #stores number of cards attempted 
totalRight = 0   #stores number of correct guesses 
loadCards(answer, fact) #call loadcards() and pass it both arrays 
numCards = len(answer) #find number of cards loaded 

而且,把你的程序称为main函数内的整点,因此您可以只运行,如果你直接调用该文件,该文件的功能,你也许应该将

if __name__ == '__main__': 
    main() 

在你的代码的底部,而不是仅仅

main() 

input()使用一般(或除非你使用Python3以后它是一样的raw_input())被视为危险,由于这样的事实:它评估输入。你应该自己处理型喜欢的东西,如果你想要一个整数,

foo = int(raw_input('Input a number: ')) 

(注意的的raw_input返回的是一个字符串,所以如果你想要一个字符串,你不必做任何事情)