0

I'm trying to run a program that administers a quiz based on the contents of a text file. My code looks like this:

This program administers a quiz on US states, capitals, and governors

global for how many questions

HOW_MANY_QUESTIONS=10

open file with answers

answers_file=open('quiz_answers.txt','r')

open file to add results at end

results_file=open('quiz_results.txt','w')

def main():

#dictionary with quiz answers
answer_dictionary={}

#run create dictionary function, result is the answer dictionary
answer_dictionary=create_dictionary(answers_file)

#close the files
answers_file.close()
results_file.close()

go_again='yes'

while go_again=='yes':

    #get name
    name=input('Enter your name: ')

    #initialize count variable
    count=0

    #accumulator for number of correct answers
    number_correct=0

    for count in range(0,HOW_MANY_QUESTIONS+1):


        #pick question function
        answer=pick_question(answer_dictionary)

        if answer:

            number_correct+=1

    results_file.write(name,number_correct)



    go_again=input('Go again? ')

def create_dictionary(infile):

#establish a dictionary 
dictionary={}

#initialize a key
key=0

#read the first line that is a state
state=infile.readline()

#strip the new line
state=state.rstrip('\n')

#while the line is not blank
while state!='':

    #add it as the dictionary key
    key=state

    #read the capital line
    capital=infile.readline()

    #strip new line
    capital=capital.rstrip('\n')

    #read the govenror line
    governor=infile.readline()

    governor=governor.rstrip('\n')

    #assign a list with capital and governor to the state key
    dictionary[key]=[capital,governor]

return dictionary

def pick_question(dictionary):

#make a list containing the keys
keys=[]

#isolate the keys and append to the key list
for key in dictionary.keys():
    keys.append(key)

#import random
import random

#initialize an index for keys list
key_index=0

#randomly select the state to ask about
state_selection=random.randint(0,len(keys)-1)


key_index=state_selection

#assign value to capital and governor to randomly
#select which question to ask
capital=1
governor=2


#randomly select whether to ask for the governor or the capital
question_choice=random.randint(capital,governor)

#if the question choice is for capital
if question_choice==capital:

    #ask the capital question 
    question=input('What is the capital of',keys[key_index],'? ')

    #if the answer is a value for that key
    if question in keys[key_index]:

        #it is correct
        answer=True
#if the question choice is for governor
elif question_choice==governor:

    #ask the governor question
    question=input('Who is the governor of',keys[key_index],'?')

    #if the answer is a value for that key
    if question in keys[key_index]:

        #it is correct
        answer=True

return answer

main()

When I run the program, the error I get is:

state=infile.readline() File"/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/encodings/ascii.py", line 26, in decode return codecs.ascii_decode(input, self.errors)[0] UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 36: ordinal not in range(128)

So the problem stems from reading that first line of the text file; what is the issue and how can I fix it? I've never seen this error before. Thanks in advance!

Brett Cohen
  • 29
  • 2
  • 4

1 Answers1

0

Wherever you open infile you will need to open it with the correct encoding. The details of how to do this:

Most likely the issue is that you are using Python 2.7 and attempting to open a utf-8 file.

Nick Chapman
  • 4,402
  • 1
  • 27
  • 41