Posted by: Sourav | March 29, 2017

Quiz game with scoring in Python 2.7,Python teacher Sourav,Kolkata 09748184075


#trivia game by Sourav Bhattacharya
try:
game_file=open(“trivia.txt”,”r”)
except IOError as e:
print ‘unable to open the file trivia.txt ending program \n’,e
raw_input(“press any key to exit \n”)
sys.exit()
else:
pass
line=game_file.readline()
line=line.replace(“/”,”\n”)
line=line[0:len(line)-2]
title=line

print “\t\tWelcome to Trivia Challenge!\n”
print “\t\t”, title, “\n”
score=0
count=0
while 1:

line=game_file.readline()
line=line.replace(“/”,”\n”)
line=line[0:len(line)-2]
category=line
if category == ”:
break
count+=1
print category
line=game_file.readline()
line=line.replace(“/”,”\n”)
line=line[0:len(line)-2]
question=line
print question
answer=[]
for i in range(0,4):
line=game_file.readline()
line=line.replace(“/”,”\n”)
line=line[0:len(line)-2]
answer.append(line)
for i in range(0,4):
print “\t”,i+1,”-“,answer[i]

correct=game_file.readline()
correct=correct[0]
#print correct
answer=raw_input(“please enter correct answer 1-4”)
line=game_file.readline()
line=line.replace(“/”,”\n”)
line=line[0:len(line)-2]
if answer==correct:
score+=1
print ‘right!= ‘,line
print ‘score= ‘,score

else:
print ‘wrong!= ‘,line
print ‘score= ‘,score

print ‘\n\n’
print ‘that was the final question’
print ‘your score is ‘,score, ‘ out of ‘,count

game_file.close

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: