0

Designed a simple trivia quiz using 2 functions, but I got an error,how can I fix this?: 1 Traceback (most recent call last): File line 31, in run_quiz(Qlist) NameError: name 'Qlist' is not defined

Here is the code: enter image description here ***from random import shuffle print('Welcome to the fun quiz!')

filename=input('Please enter the filename(quiz.txt)to get started:' )
with open(filename,'rb') as f:
lines=f.readlines()

numQ=int(input('How many questions would you like to answer (10-15)?'))

def questions(numQ):
'''This function shuffles the quiz bank and create a question list for the users to answer'''
shuffle(lines)
Qlist=lines[:numQ]
return Qlist questions(numQ)

def run_quiz(Qlist):
'''Ask the user questions, determine whether the answer is correct, and count the correct answers.'''
right=0
for line in Qlist:

question, rightAnswer=line.strip().split('\t')

answer=input(question+' ')

if answer.lower()==rightAnswer:

print('Correct!')

right+=1

else:

print('Incorrect.The right answer is', rightAnswer)
return print('You got',right,'out of',numQ,'which is',right/numQ*100,'%.') run_quiz(Qlist)***

2
  • From what I see, you are calling run_quiz(Qlist) but you have not defined Qlist yet as it is only defined when you save the returned value from questions(numQ), and then pass that returned value into run_quiz() Commented Apr 20, 2020 at 21:38
  • how can i save the return value? Commented Apr 20, 2020 at 21:50

1 Answer 1

0

You can use the return value like so. As Qlist is returned from questions, you call that function and pass it as a parameter to run_quiz

filename=input('Please enter the filename(quiz.txt)to get started:' )
with open(filename,'rb') as f:
    lines=f.readlines()

numQ=int(input('How many questions would you like to answer (10-15)?'))

def questions(numQ):
    shuffle(lines)
    Qlist=lines[:numQ]
    return Qlist

def run_quiz(Qlist):
    right=0
    for line in Qlist:
        question, rightAnswer=line.strip().split('\t')
        answer=input(question+' ')
        if answer.lower()==rightAnswer:
            print('Correct!')
            right+=1
        else:
            print('Incorrect.The right answer is', rightAnswer)
    return print('You got',right,'out of',numQ,'which is',right/numQ*100,'%.')

run_quiz(questions(numQ))
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much!!!! but another error occurred, could you take a look and help me with it? : Traceback (most recent call last): File "/Users/zikkki/Desktop/PSU/CS101/program implementation.py", line 30, in <module> run_quiz(questions(numQ)) File "/Users/zikkki/Desktop/PSU/CS101/program implementation.py", line 22, in run_quiz question,rightAnswer=line.strip().split('\t') TypeError: a bytes-like object is required, not 'str'
You are reading the file in binary mode. with open(filename, 'rb'). Just use 'r' instead of 'rb'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.