请原谅我,如果这有点分散,当我说我已经在这个项目上工作超过13个小时并且我严重睡眠不足时,我并不夸张。这是我的第四次修订,老实说,我不知道该怎么做,所以如果有人能帮助我,我将不胜感激。我对编程老师的介绍要求我们制作一张闪存卡"从他的模板学习程序。我在Windows 7机器上使用Idle 3.3.3。
#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中并从那里运行,我会得到#34; SyntaxError:在编译单个语句时找到多个语句"。这两个文件都没有改变,但早些时候它告诉我"导入随机"有问题。
提前感谢您的帮助。
答案 0 :(得分:2)
我快速看了一下,对我来说似乎没什么好看的。我将input()更改为raw_input()(在您的代码中有两个)并注意到当你可能意味着单个时,你有一个双等于
第36行:
totalRight == totalRight + 1
更改为
totalRight = totalRight + 1
修正了你的正确答案计数器和第68行:
line3 = infile.readline ()
更改为
line1 = infile.readline ()
否则它永远会被你的while循环捕获。我刚刚复制了第54行:
line1 = infile.readline ()
并粘贴它,所以它有两次添加另一个readline()调用,只是一种懒惰的方式来跳过文本文件中的第一行,因为它似乎是一个注释而不是答案和问题的一部分。您可能不想这样做,只需从文本文件中删除评论即可。 = B
通过这些更改,它似乎对我来说很好。
答案 1 :(得分: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的返回是一个字符串,所以如果你想要一个字符串,你不需要做任何事情)