I am trying to make a 20 questions game and I'm having trouble getting it to check if I have a certain variable inside of two different arrays.
Example:
wins = 0
loses = 0
def updateQandA(*args):
global wins, loses, answers, correctanswers
if correctanswers != answers:
loses += 1
if correctanswers == answers:
wins += 1
answers = [Answers4, Answers8, Answers12
These variable do exist in my code:
correctanswers = [A2, A5,A10, A15, A20, A24, A25]
answers1 = OptionMenu(root, var, *Answers4, command = updateQandA).grid()
There are two arrays with a menu button and 4 options. One of the options in this option menu is A2. What is happening is that no matter what I pick, it just adds one to loses
. The goal is to get it to where it recognizes that there is a correct answer in one of the 4 options and when I choose the correct answer, it adds one to wins
.
If I understand your question correctly, and all you want to do is check whether the answer exists in the other array, you could use a for loop with an if statement:
for ans in answers:
if ans in correctanswers:
wins += 1
else:
loses += 1
If both arrays are of the same length, and the index in 'answer' corresponds with the index in 'correctanswer', you can use range():
for i in range(len(answers)):
if answers[i] == correctanswers[i]:
wins += 1
else:
loses += 1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.