Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python Help needed!

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.

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()
 

Buy us a coffee!

Back
Top Bottom