English 中文(简体)
如何将用户选择与项目清单和毛豆价值清单进行比较?
原标题:How to compare a user choice with a list of item and a list of boolean value?

I have this code, and i want to compare if the choice of a user (from the list "choix") is equal to the list "bonne_reponse". the user see only the question and a choice of responce.

For exemple: choix = [tomatos, apple, grappe, citrus] bonne_reponse = [True, False, False, False]

“问题? 在这份清单中,什么没有结果?

1 - tomatos 2 - apple 3 - grappe 4 - citrus

页: 1

好的答案是1,但一是接受alway的“错误回答”(Mauvaise reponse),并且认为这是一个“好答案”(Bonne reponse)。

The data i use come like this: {..."questions": [{"titre"`:"...?", "choix": [["tomatos", true], ["apple", false], ........]]}], ...}

我的法典中有一部分是:

    def FromData(data):
    # séparer la liste "choix" en 2 listes, une pour les choix et une pour les bonnes réponses
    choix = []
    bonne_reponse = []

    for i in range(len(data["choix"])):
        choix.append(data["choix"][i][0])
        bonne_reponse.append(data["choix"][i][1])
        # transformer la liste "bonne_reponse" en int pour pouvoir la comparer avec la réponse de l utilisateur
        bonne_reponse[i] = int(bonne_reponse[i])

        # print(data["choix"])
        # print(choix)
        # print(bonne_reponse)
        # q = Question(data["titre"], data["choix"][i][0], data["choix"][i][1])
    q = Question(data["titre"], choix, bonne_reponse)

    return q

def poser(self):
    print("QUESTION")
    print("  " + self.titre)
    for i in range(len(self.choix)):
        print("  ", i+1, "-", self.choix[i])

    print()
    resultat_response_correcte = False
    reponse_int = Question.demander_reponse_numerique_utlisateur(1, len(self.choix))
    if self.choix[reponse_int-1] == self.bonne_reponse:
        print("Bonne réponse")
        resultat_response_correcte = True
    else:
        print("Mauvaise réponse")

    print()
    return resultat_response_correcte

非常感谢。

问题回答

你们需要使用字典来储存自己选择的包料。

choices = { tomatos :True,  apple :False,  grappe :False,  citrus :False}

q= Which of the following is  not a fruit?
 
for choice in choices.keys():
    q+=choice+ 
 

choosen=input(q)
if choices[choosen]==True:
    print( correct answer )
else:
    print( wrong answer )




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签