Reputation: 133
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!
Upvotes: 1
Views: 45
Reputation: 1239
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")
Upvotes: 1