原谅我,如果这出来有点散漫,当我说我已经在这个程序超过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:编译单个语句时发现的多个语句”。这两个文件都没有改变,但早些时候它告诉我“随机导入”存在问题。
在此先感谢您的帮助。
它也有助于包含实际的回溯,不仅仅是例外,或者更糟糕的是,“出现了问题”。回溯有重要的信息,包括指出错误发生的位置和位置,这使得它更容易帮助你。 –
请更改您的问题,提供错误追溯,相关代码,预期输入/输出。你必须帮助我们帮助你。 – sshashank124
@Wooble:有效点。正如我所说,我没有睡太多。我回来编辑我的帖子,但其他人已经有了。 – user3598983