简体   繁体   English

检查项目在一个列表到另一个列表中的位置

[英]checking the position of an item in one list to another list

I am trying to make a trivia game, the only problem is I am having a hard time checking for the right answer. 我正在尝试制作琐事游戏,唯一的问题是我很难检查正确的答案。 Here is my code for one of the questions 这是我的其中一个问题的代码

Question2 = random.choice(mylist)
print (Question2)
Userinput = input()
if(Question2.position == Question2answer.position):
    print('Yes, that is correct!')
else:
    print('Sorry, wrong answer')
mylist.remove(Question2)

I am trying to check if what the user put for question 2 was the answer to question 2 and not 4 by checking the positions in the list. 我试图通过检查列表中的位置来检查用户对问题2的回答是问题2的答案,而不是问题4的答案。

The easy solution is to use the right data type for the job. 简单的解决方案是为作业使用正确的数据类型。

For example, if your mylist were a list of (question, answer) pairs, instead of having two separate lists; 例如,如果您的mylist(question, answer)对的列表,而不是两个单独的列表;

Question2, Answer2 = random.choice(mylist)
print(Question2)
Userinput = input()
if Userinput == Answer2:
    print('Yes, that is correct!')
else:
    print('Sorry, wrong answer')
mylist.remove((Question2, Answer2))

Or, alternatively, with a dictionary instead of a list: 或者,或者用字典而不是列表:

Question2 = random.choice(mydict)
print(Question2)
Userinput = input()
if Userinput == mydict[Question2]:
    print('Yes, that is correct!')
else:
    print('Sorry, wrong answer')
del mylist[Question2]

Why is a dict better? 为什么字典更好? Well, for one thing, with a list, you have to repeatedly search through the list to find the value you want—eg, mylist.remove starts at the beginning and compares each element to your value until it finds the right one. 好吧,一方面,对于列表,您必须反复搜索列表以查找所需的值,例如, mylist.remove开始,并将每个元素与您的值进行比较,直到找到正确的值。 Besides being slow, and overly complicated, this does the wrong thing if you can ever have duplicate values (eg, try a = [1, 2, 3, 1] , then value = a[0] , then a.remove(value) and see what happens…). 如果您有重复的值,则除了速度慢和过于复杂之外,如果您有重复的值(例如,尝试a = [1, 2, 3, 1] a.remove(value) a = [1, 2, 3, 1] ,然后value = a[0] ,然后a.remove(value) ,看看会发生什么……)。


But if you can't change the data structures, you can always use zip to zip up a pair of separate lists into a single list of pairs on the fly: 但是,如果您无法更改数据结构,则始终可以使用zip即时将一对单独的列表zip为一个成对的单个列表:

Question2, Answer2 = random.choice(zip(mylist, myanswers))
print(Question2)
Userinput = input()
if Userinput == Answer2:
    print('Yes, that is correct!')
else:
    print('Sorry, wrong answer')
mylist.remove(Question2)
myanswers.remove(Answer2)

You could use namedtuple as data container. 您可以使用namedtuple作为数据容器。

from collections import namedtuple
import random


Question = namedtuple('Question', ['question', 'answer'])
questions = [
    Question('Question 1', 'Answer 1'),
    Question('Question 2', 'Secret'),
    Question('Question 3', '3'),
]


q = random.choice(questions)
print("%s ?" % q.question)
user_input = raw_input().strip()


if(q.answer == user_input):
    print('Yes, that is correct!')
else:
    print('Sorry, wrong answer')

questions.remove(q)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM