Question

Forgive me if this comes out a bit scatter-brained, I'm not exaggerating when I say I've been working on this program for over 13 hours now and I am seriously sleep deprived. This is my 4th revision and I honestly don't know what to do anymore, so if anyone can help me, it would be greatly appreciated. My introduction to programming teacher wanted us to make a "flash card" study program from his template. I am using Idle 3.3.3 on a windows 7 machine.

#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()

When I run the program nothing actually happens, but when I try to close the shell window afterwards, it tells me that the program is still running and asks if I want to kill it.

Debugger shows me no information when I try to check it, also.

However, if I copy the code into the shell and run it from there, I get "SyntaxError: multiple statements found while compiling a single statement". Neither file has changed, but earlier it was telling me there was a problem with "import random".

Thanks in advance for any help.

Was it helpful?

Solution

I took a quick look and it mostly seems okay to me. I changed input() to raw_input() (two of them in your code) and noticed you had a double equals when you probably meant a single one

line 36:

totalRight == totalRight + 1

changed to

totalRight = totalRight + 1

which fixes your correct answer counter and line 68:

line3 = infile.readline ()

changed to

line1 = infile.readline ()

else it gets caught in your while loop forever. And I just copied line 54:

line1 = infile.readline ()

and pasted it so it is there twice to add another readline() call, just a lazy way of skipping the first line in your text file, since it seems to be a comment and not part of the answers and questions. You probably don't want to do that and just remove the comment from your text file. =b

With those changes, it seems to work fine for me.

OTHER TIPS

Since this is for a class (and I can't only comment, I can just answer) I want to add that there actually is such a thing as too many comments

These comments (and to be honest, most of your comments) are distracting and unnecessary

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

Also, the whole point of putting your program inside of a function called main is so you can run that function only if you are calling that file directly and you should probably put

if __name__ == '__main__':
    main()

at the bottom of your code instead of just

main()

Use of input() is generally considered dangerous (unless you're using Python3 or later where it is the same as raw_input()) due to the fact that it evaluates the input. You should handle the type yourself with something like, if you want an integer,

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

(Note that the return of raw_input is a string, so if you want a string you don't have to do anything)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top