J'écris un programme qui est une sorte de mini-jeu d'aventure texte, mais je semble rencontrer des problèmes avec mon code. Je suis relativement nouveau dans la programmation dans son ensemble, donc si vous pouviez expliquer vos réponses en profondeur, ce serait d'une grande aide.

Voici le code:

#text_adventure_game.py
#python_ver == 3.5

verbs = ["get",
         "put",
         "drop",
         "use",
         "enter",
         "leave",
         "walk",
         "search",
         "attack",
         "defend"]

nouns = ["journal",
         "magnifier",
         "glasses",
         "knife",
         "kite",
         "table",
         "chair",
         "key"]

inventory = ""

user_input = input("What would you like to do?\n")

#checking if there are a set of two instructions
if(len(user_input.split())==2):
    counter = 0
    for token in user_input.split():
        print(token)
        print(counter)
        if(counter==0):
            if(token not in verbs):
                print("I did not understand the verb entered.")
                #removing square brackets and single quote marks from the array
                print("Please use:", ", ".join(verbs))
                counter = counter + 1
            elif(token in verbs):
                print("Recognized verb entered.")
                counter = counter + 1
        if(counter==1):
            if(token not in nouns):
                print("I did not understand the noun entered.")
                print("Please use:", ", ".join(nouns))
                counter = counter + 1
            elif(token in nouns):
                print("Recognized verb entered.")
                counter = counter + 1

Mon problème est qu'il ne reconnaît pas les noms que j'entre dans le tableau "noms".

Voici comment il compile:

>>> 
What would you like to do?
get journal
get
0
Recognized verb entered.
I did not understand the noun entered.
Please use: journal, magnifier, glasses, knife, kite, table, chair, key
journal
2

S'il existe des moyens plus efficaces de faire des choses comme celle-ci, cela aiderait également.

Merci.

1
kitsibu 8 mars 2016 à 15:01

4 réponses

Meilleure réponse

Il n'y a pas besoin d'une boucle for et d'un compteur. Vous pouvez plus simplement faire:

verbs = ["get",
         "put",
         "drop",
         "use",
         "enter",
         "leave",
         "walk",
         "search",
         "attack",
         "defend"]

nouns = ["journal",
         "magnifier",
         "glasses",
         "knife",
         "kite",
         "table",
         "chair",
         "key"]

inventory = ""

user_input = input("What would you like to do?\n")

#checking if there are a set of two instructions
action, obj = user_input.split()

if(action not in verbs):
    print("I did not understand the verb entered.")
    print("Please use:", ", ".join(verbs))
else:
    print("Recognized verb entered.")

    if(obj not in nouns):
        print("I did not understand the noun entered.")
        print("Please use:", ", ".join(nouns))
    else:
        print("Recognized noun entered.")

Si vous souhaitez le répéter après l'échec, vous pouvez placer le code sous un bloc while.

Quoi qu'il en soit, le problème dans votre code d'origine est que vous devez insérer un continuer si le premier verbe est correct:

verbs = ["get",
         "put",
         "drop",
         "use",
         "enter",
         "leave",
         "walk",
         "search",
         "attack",
         "defend"]

nouns = ["journal",
         "magnifier",
         "glasses",
         "knife",
         "kite",
         "table",
         "chair",
         "key"]

inventory = ""

user_input = input("What would you like to do?\n")

#checking if there are a set of two instructions
if(len(user_input.split())==2):
    counter = 0
    for token in user_input.split():
        print("1", token)
        print(counter)
        if(counter==0):
            if(token not in verbs):
                print("I did not understand the verb entered.")
                #removing square brackets and single quote marks from the array
                print("Please use:", ", ".join(verbs))
                counter = counter + 1
            elif(token in verbs):
                print("Recognized verb entered.")
                counter = counter + 1
                continue
        if(counter==1):
            if(token not in nouns):
                print("I did not understand the noun entered.")
                print("Please use:", ", ".join(nouns))
                counter = counter + 1
            elif(token in nouns):
                print("Recognized verb entered.")
                counter = counter + 1
1
Marco Sulla 8 mars 2016 à 12:18

Le vrai problème est la position de votre compte. Voici le code changé. Bien sûr, vous pouvez également utiliser un continu sur le premier if counter == 0, vous pouvez changer l'événement if counter == 1 en elif counter == 1.

#text_adventure_game.py
#python_ver == 3.5

verbs = ["get",
         "put",
         "drop",
         "use",
         "enter",
         "leave",
         "walk",
         "search",
         "attack",
         "defend"]

nouns = ["journal",
         "magnifier",
         "glasses",
         "knife",
         "kite",
         "table",
         "chair",
         "key"]

inventory = ""

user_input = input("What would you like to do?\n")

#checking if there are a set of two instructions
if(len(user_input.split())==2):
    counter = 0
    for token in user_input.split():
        print(token)
        print(counter)
        if(counter==0):
            if(token not in verbs):
                print("I did not understand the verb entered.")
                #removing square brackets and single quote marks from the array
                print("Please use:", ", ".join(verbs))
                counter = counter + 1
            elif(token in verbs):
                print("Recognized verb entered.")
        if(counter==1):
            if(token not in nouns):
                print("I did not understand the noun entered.")
                print("Please use:", ", ".join(nouns))
                counter = counter + 1
            elif(token in nouns):
                print("Recognized noun entered.")
        counter = counter + 1
0
Walter_Ritzel 8 mars 2016 à 13:02

Le problème est que vous augmentez la variable de compteur dans "if (counter == 0):" de 0 à 1. Puisque vous avez une autre instruction if juste après ("if (counter == 1):") les deux instructions if s'exécuteront au cours de la même itération ce qui signifie qu'avec une entrée "get key" vous vérifieriez si "get" est dans les verbes mais aussi si "get" est dans les noms. Je proposerais de changer le second if en un elif comme celui-ci:

verbs = ["get",
         "put",
         "drop",
         "use",
         "enter",
         "leave",
         "walk",
         "search",
         "attack",
         "defend"]

nouns = ["journal",
         "magnifier",
         "glasses",
         "knife",
         "kite",
         "table",
         "chair",
         "key"]

inventory = ""

user_input = input("What would you like to do?\n")

#checking if there are a set of two instructions
if(len(user_input.split())==2):
    counter = 0
    for token in user_input.split():
        print(token)
        print(counter)
        if(counter==0):
            if(token not in verbs):
                print("I did not understand the verb entered.")
                #removing square brackets and single quote marks from the array
                print("Please use:", ", ".join(verbs))
                counter += 1
            else: #this can be simplified as an else since the booleans you check are mutually exclusive
                print("Recognized verb entered.")
                counter += 1
        elif(counter==1): #this now only executes if counter isn't 1 to make sure it only triggers on the second word in the array
            if(token not in nouns):
                print("I did not understand the noun entered.")
                print("Please use:", ", ".join(nouns))
                counter += 1
            else:
                print("Recognized noun entered.")
                counter += 1
0
K. Gl. 8 mars 2016 à 12:25

Vous incrémentez le compteur lorsque vous trouvez le verbe, donc la ligne if (compteur == 1) s'exécute avant que le jeton ne soit incrémenté (le jeton contient toujours 'get' pendant que le bloc compteur == 1 est en cours d'exécution).

La solution la plus simple consisterait à

if(counter==1):

Dans un elif:

elif(counter==1):

Ou si vous souhaitez être gentil et explicite, vous pouvez ajouter un «continuer» chaque fois que vous avez réussi à traiter un jeton, et la boucle passera à l'itération suivante sans traiter plus le corps de la boucle:

if(counter==0):
    if(token in verbs):
        print("Recognized verb entered.")            
    else
        print("I did not understand the verb entered.")
        #removing square brackets and single quote marks from the array
        print("Please use:", ", ".join(verbs))

    counter = counter + 1
    continue

(Remarque, j'ai également modifié votre test if / elif, car vous testez deux conditions booléennes mutuellement exclusives - 'in' et 'not in'.)

J'espère que cela pourra aider.

0
srowland 8 mars 2016 à 12:17