Thomas
New Coder
I have this quiz game I am making and it always says 'Incorrect' when I put in the answer.
It runs with functions and files.
It runs with functions and files.
Python:
import random
repeat = 'True'
questionNumber = 1
def questions():
questionList = [0,6]
questionQuestion = random.choice(questionList)
intOfQuestion = int(questionQuestion)
file = open('questions')
content = file.readlines()
#question/answer values
question = content[intOfQuestion]
answerA = content[intOfQuestion + 1]
answerB = content[intOfQuestion + 2]
answerC = content[intOfQuestion + 3]
answerD = content[intOfQuestion + 4]
correctAnswer = content[intOfQuestion + 5]
#print questions
print('''Question {}:
{}
A = {}
B = {}
C = {}
D = {}'''.format(questionNumber, question, answerA, answerB, answerC, answerD))
userAnswer = input("What's the answer? ").upper()
if userAnswer == correctAnswer:
print("Correct")
else:
print("Incorrect.")
def quiz():
print()
print("Hello and welcome to the Quiz!")
questions()
def questionsCreate():
q = input("Enter a question: ")
a = input("Enter answer A: ")
b = input("Enter answer B: ")
c = input("Enter answer C: ")
d = input("Enter answer D: ")
correct = input("Enter the correct answer letter: ")
with open("questions", "a") as a_file:
a_file.write(str(q))
a_file.write("\n")
a_file.write(str(a))
a_file.write("\n")
a_file.write(str(b))
a_file.write("\n")
a_file.write(str(c))
a_file.write("\n")
a_file.write(str(d))
a_file.write("\n")
a_file.write(str(correct.upper()))
a_file.write("\n")
def start():
user = input("Enter your username: ")
#need to add checker here
quiz()
def create():
#get username
new_username = input("Enter your username to be created: ")
#check for username in file
usernames = open("username", "r")
index = 0
flag = 0
#looping through file
for line in usernames:
index += 1
# checking string is present in line or not
if new_username in line:
flag = 1
break
#output
if flag == 1:
print('Username "{}" already exists or is not valid.'.format(new_username))
else:
#write to file
with open("username", "a") as a_file:
a_file.write(new_username)
a_file.write("\n")
a_file.write('0')
a_file.write("\n")
#success
print("The username '{}' has been created.".format(new_username))
def check():
#get username
check_username = input("Enter a username to check: ")
#check for username in file
usernames = open("username", "r")
index = 0
flag = 0
#looping through file
for line in usernames:
index += 1
# checking string is present in line or not
if check_username in line:
flag = 1
break
#output
if flag == 1:
print('Username "{}" found.'.format(check_username))
else:
print('Username "{}" not found.'.format(check_username))
def developer():
print('''[1]: Create Questions
[2]: Clear Usernames and Scores
[3]: Get Usernames''')
developerGo = input("Where would you like to go? ")
#where to go
if developerGo == '1':
questionsCreate()
elif developerGo == '2':
clear()
elif developerGo== '3':
listUsers()
def menu():
#menu
print('''[1]: Start Quiz
[2]: Create Username
[3]: Check Username
[4]: Developer mode''')
toGo = input("Where would you like to go? ")
#where to go
if toGo == '1':
start()
elif toGo == '2':
create()
elif toGo == '3':
check()
elif toGo == '4':
developer()
else:
print("Sorry I don't understand.")
print()
menu()