Опечатки при вводе все еще соответствуют условиям для текстовой RPG-игры с Python 2.7 на UNIX - PullRequest
0 голосов
/ 25 февраля 2020

Я уже месяц учусь кодировать с помощью учебника «Learn Python The Hard Way». Пока это было очень весело. Я бросил себе вызов в создании текстовой RPG-игры.

В настоящее время я переписываю код для улучшения читабельности и обслуживания.

Я столкнулся с проблемой:

Если Я набираю выбор ввода «открыть дверь» (обратите внимание на ошибку опечатки), условия все еще выполняются, и я получаю ожидаемый отпечаток, как если бы он не имел ошибки опечатки.

Любые идеи о том, что Я сделал неправильно?

Спасибо за помощь:)

PS: У меня есть еще один вопрос, но я решил, что мне нужно будет задать его в отдельном файле вопросов.

prison_key = False
def test():
    global prison_key
    curr_prompt = "What do you do?"
    print curr_prompt
    choice = raw_input("> ").lower()
    while "quit" not in choice:
        if "go" in choice:
            if "cellar" in choice:
                print "cellar"
            elif "gravel" in choice or "path" in choice:
                print "gravel path"
            elif "prison" in choice and prison_key:
                print "You enter the prison."
            elif "prison" in choice: 
                print "The door is locked."
            else:
                print "Invalid"
        elif "search" in choice:
            if "search" == choice:
                print "invalid"
            elif "prison":
                print "The door is closed."
            elif "bucket" in choice:
               print "The bucket is empty."
            elif "keg" in choice:
                prison_key = True
                print "You find a key."
            elif "door" in choice and ("heavy" in choice or "steel" in choice or "metal" in choice):
                print "It looks like a prison door"
            elif "search door" == choice:
                print "Which one?"
            else:
                print "You find nothing."
        elif "open door" == choice:
                print "which one?" 
        elif "open" in choice:
            if "door" in choice and ("wooden" in choice or "cellar" in choice):
                print "gravel path"
            elif "door" in choice and ("steel" in choice or "metal" in choice or "door" in choice or "heavy" in choice or "prison" in choice) and prison_key:
                print "You open the prison"
            elif "door" in choice and ("steel" in choice or "metal" in choice or "door" in choice or "heavy" in choice or "prison" in choice):
                print "the door is locked"
            elif "prison" in choice and prison_key:
                print "You enter the prison."
            elif "prison" in choice:
                print "the door is locked."
            else:
                print "invalid"
        elif "drink" in choice and "wine" in choice:
            print "You alcoholic."
        else:
            print "invalid"
        print curr_prompt
        choice = raw_input("> ").lower()
    exit(0)

test()

Ответы [ 2 ]

1 голос
/ 25 февраля 2020

так нашел вашу проблему. В состоянии elif для

elif "door" in choice and ("steel" in choice or "metal" in choice or "door" in choice or "heavy" in choice or "prison" in choice):

Вы снова проверяете дверь, и таким образом она проходит условие if. То же относится и к этому условию

elif "door" in choice and ("steel" in choice or "metal" in choice or "door" in choice or "heavy" in choice or "prison" in choice) and prison_key:                 

    print "the door is locked"
0 голосов
/ 25 февраля 2020

Я думаю, что ошибка в том, что вы указали "door" in choice во второй строке вашего and предложения:

elif "door" in choice and ("steel" in choice or "metal" in choice or "door" in choice or "heavy" in choice or "prison" in choice) and prison_key:
            print "You open the prison"

То же самое относится к следующим elif.

Хороший проект между прочим.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...