[英]I wrote a simple python game but functions in that are not executing
[英]wrote a simple quiz code using two functions in python but a list is not defined
使用 2 个函数设计了一个简单的琐事测验,但出现错误,我该如何解决这个问题?: 1 Traceback(最近一次调用最后一次):文件第 31 行,在 run_quiz(Qlist) 中 NameError: name 'Qlist' is not defined
这是代码:在此处输入图像描述***from random import shuffle print('Welcome to the fun quiz!')
filename=input('请输入文件名(quiz.txt)开始:')
使用 open(filename,'rb') 作为 f:
行=f.readlines()
numQ=int(input('你想回答多少问题 (10-15)?'))
定义问题(numQ):
'''这个 function 随机排列测验库并创建一个问题列表供用户回答'''
洗牌(行)
Qlist=lines[:numQ]
返回 Qlist 问题(numQ)
def run_quiz(Qlist):
'''向用户提问,判断答案是否正确,统计正确答案。'''
右=0
对于 Qlist 中的行:
问题,rightAnswer=line.strip().split('\t')
答案=输入(问题+'')
如果 answer.lower()==rightAnswer:
print('正确!')
右+=1
别的:
print('不正确。正确答案是', rightAnswer)
return print('You got',right,'out of',numQ,'这是',right/numQ*100,'%.') run_quiz(Qlist)***
您可以像这样使用返回值。 当 Qlist 从问题中返回时,您将其称为 function 并将其作为参数传递给 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))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.