So i have a python script. It is a quiz game imported from a text file. All i ha
ID: 3770786 • Letter: S
Question
So i have a python script. It is a quiz game imported from a text file. All i have left to figure out is how to make it the script restart. So the person can do the quiz again if he chooses this is what i have so far.
# Quiz Project #1
# Begin by reading in the quiz.txt file into a list
import random
infile = open("quiz.txt", 'r')
question = [line.rstrip() for line in infile]
infile.close()
score=0
i = 1
# We know that there are six lines per question
# Picking some at random, the questions could begin at lines:6,18,36,54,66
qNum = random.randrange(6,600,6)
# We are now ready to ask 10 questions
print("Welcome to the Quiz")
print(" ",question[qNum+1])
print("A:",question[qNum+2])
print("B:",question[qNum+3])
print("C:",question[qNum+4])
print("D:",question[qNum+5])
print("======================")
userChoice=str.upper(input("Enter your answer: "))
if userChoice==question[qNum]:
print("You are Correct!")
score+=1
else:
print(" Sorry, the correct answer was ",question[qNum])
print("======================")
# Now move on to the next question
qNum = random.randrange(6,600,6)
# We are now ready to ask 10 questions
print("Welcome to the Quiz")
print(" ",question[qNum+1])
print("A:",question[qNum+2])
print("B:",question[qNum+3])
print("C:",question[qNum+4])
print("D:",question[qNum+5])
print("======================")
userChoice=str.upper(input("Enter your answer: "))
if userChoice==question[qNum]:
print("You are Correct!")
score+=1
else:
print(" Sorry, the correct answer was ",question[qNum])
print("======================")
# Now move on to the next question
qNum = random.randrange(6,600,6)
# We are now ready to ask 10 questions
print("Welcome to the Quiz")
print(" ",question[qNum+1])
print("A:",question[qNum+2])
print("B:",question[qNum+3])
print("C:",question[qNum+4])
print("D:",question[qNum+5])
print("======================")
userChoice=str.upper(input("Enter your answer: "))
if userChoice==question[qNum]:
print("You are Correct!")
score+=1
else:
print(" Sorry, the correct answer was ",question[qNum])
print("======================")
# Now move on to the next question
qNum = random.randrange(6,600,6)
# We are now ready to ask 10 questions
print("Welcome to the Quiz")
print(" ",question[qNum+1])
print("A:",question[qNum+2])
print("B:",question[qNum+3])
print("C:",question[qNum+4])
print("D:",question[qNum+5])
print("======================")
userChoice=str.upper(input("Enter your answer: "))
if userChoice==question[qNum]:
print("You are Correct!")
score+=1
else:
print(" Sorry, the correct answer was ",question[qNum])
print("======================")
# Now move on to the next question
qNum = random.randrange(6,600,6)
# We are now ready to ask 10 questions
print("Welcome to the Quiz")
print(" ",question[qNum+1])
print("A:",question[qNum+2])
print("B:",question[qNum+3])
print("C:",question[qNum+4])
print("D:",question[qNum+5])
print("======================")
userChoice=str.upper(input("Enter your answer: "))
if userChoice==question[qNum]:
print("You are Correct!")
score+=1
else:
print(" Sorry, the correct answer was ",question[qNum])
print("======================")
# Now move on to the next question
qNum = random.randrange(6,600,6)
# We are now ready to ask 10 questions
print("Welcome to the Quiz")
print(" ",question[qNum+1])
print("A:",question[qNum+2])
print("B:",question[qNum+3])
print("C:",question[qNum+4])
print("D:",question[qNum+5])
print("======================")
userChoice=str.upper(input("Enter your answer: "))
if userChoice==question[qNum]:
print("You are Correct!")
score+=1
else:
print(" Sorry, the correct answer was ",question[qNum])
print("======================")
# Now move on to the next question
qNum = random.randrange(6,600,6)
# We are now ready to ask 10 questions
print("Welcome to the Quiz")
print(" ",question[qNum+1])
print("A:",question[qNum+2])
print("B:",question[qNum+3])
print("C:",question[qNum+4])
print("D:",question[qNum+5])
print("======================")
userChoice=str.upper(input("Enter your answer: "))
if userChoice==question[qNum]:
print("You are Correct!")
score+=1
else:
print(" Sorry, the correct answer was ",question[qNum])
print("======================")
# Now move on to the next question
qNum = random.randrange(6,600,6)
# We are now ready to ask 10 questions
print("Welcome to the Quiz")
print(" ",question[qNum+1])
print("A:",question[qNum+2])
print("B:",question[qNum+3])
print("C:",question[qNum+4])
print("D:",question[qNum+5])
print("======================")
userChoice=str.upper(input("Enter your answer: "))
if userChoice==question[qNum]:
print("You are Correct!")
score+=1
else:
print(" Sorry, the correct answer was ",question[qNum])
print("======================")
# Now move on to the next question
qNum = random.randrange(6,600,6)
# We are now ready to ask 10 questions
print("Welcome to the Quiz")
print(" ",question[qNum+1])
print("A:",question[qNum+2])
print("B:",question[qNum+3])
print("C:",question[qNum+4])
print("D:",question[qNum+5])
print("======================")
userChoice=str.upper(input("Enter your answer: "))
if userChoice==question[qNum]:
print("You are Correct!")
score+=1
else:
print(" Sorry, the correct answer was ",question[qNum])
print("======================")
# Now move on to the next question
qNum = random.randrange(6,600,6)
# We are now ready to ask 10 questions
print("Welcome to the Quiz")
print(" ",question[qNum+1])
print("A:",question[qNum+2])
print("B:",question[qNum+3])
print("C:",question[qNum+4])
print("D:",question[qNum+5])
print("======================")
userChoice=str.upper(input("Enter your answer: "))
if userChoice==question[qNum]:
print("You are Correct!")
score+=1
else:
print(" Sorry, the correct answer was ",question[qNum])
print("======================")
# Now print out the score
print(" Your score was",score,"out of 10")
Explanation / Answer
To Restart the script you can use a while loop by setting the flag and changing the flag after the quiz is completed by taking input from the user.
# initially set flag to 1
flag=1
while flag==1:
# paste your quiz code here
print("press 1 to play again or anything else to quit")
x = int(input())
flag = x
print("thank you for playing!!! come back soon")
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.