1

So I host trivia games in my town and I was trying to write a program to allow me to calculate the scores by inputting player's answers instead of me doing it manually with pen and paper. I am trying to get started and was creating the beginning part of the code to determine if the inputted answer is correct, but I keep running into some errors.

answer1 = ["duke of cambridge", "prince of wales", 3]
answer2 = ["android", 5]

for i in range(2):
    arrayToFind = "answer" + str(i+1)
    answers = input("answers here:").split(",")
    iteration = len(answers)
    for x in range(iteration):
        if(answers[x] == arrayToFind[x]):
            print("right")
        else:
            print("wrong")

I created the two matrices and made a loop to go through and compare the answers. What I am having a problem with is finding a way to have the loop check a different array each time. For instance, on the first run, it would compare inputted answers to those in the answer1 array. However, the second run would compare to the answer2 array.

I thought about using 2D arrays, but it didn't seem as easy as it is in some other languages like Java. Any tips on how to make the code work? Right now, arrayToFind is a string, but I want it to be the keyword almost, like if arrayToFind = answer1, use the answer1 array.

Thanks in advance!

1
  • Also, the numbers at the end of the matrices are point values - they aren't important for this part, as I haven't gotten around to implementing them yet. Commented Nov 27, 2017 at 0:36

1 Answer 1

1

IMO, the correct way to do this would be a Python 2D list. It's pretty simple:

solutions = [
    ["ans1", "ans2", "ans3", 2],
    ["ans4", "ans5", 3]
    # etc.
] # If you want "ans1", you can use solutions[0][0]. For "ans3": solutions[0][2]

for question in solutions:
    answers = input("answers here:").split(",")
    if(answers == question): # This checks to see if the lists are the same, e.g. ["foo", 3, "bar"] == ["foo", 3, "bar"] returns True.
        print("right")
    else:
        print("wrong")
Sign up to request clarification or add additional context in comments.

2 Comments

This helped a lot. It didn't do exactly as I wanted, because the answers == question line checked everything, which meant I would have to enter the point value each time I entered a user's answers. However, I told it to iterate for every item in the answers, and compare that to the item at the same index of the question, therefore making it not check the point value. Thanks for the help m8.
Glad I could help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.