Условные не работают в моем коде Python

Это попытка простого текстового приключения. Мне не удается заставить условия работать так, как я их хочу. Когда вводится пользовательский ввод, например «слева», он говорит, что «left» не определен, хотя я перечислил его там. Вот код.

from time import sleep name = raw_input("What is your name? ") live = raw_input("Where do you live? ") print "%s. You have just come to your senses." % (name) print "You are in %s, lying in the road." % (live) sleep (5) from datetime import datetime now = datetime.now() print "You look at your watch. There's a crack in the screen." sleep (5) print "Surely it cannot be %s/%s/%s at %so clock..." % \ (now.day, now.month, now.year, now.hour) sleep (5) print "There is light blotting orange onto your closed eyes." sleep (7) print "You open your eyes." sleep (5) print "you see the road. Scarred, cracked and peppered with dead birds." sleep (5) print "Smoke thins out over the horizon." sleep (5) print "There is a house you are familiar with, to your right." sleep (3) print "There is a road leading uphill to your left." sleep (3) print "Where do you turn to?" answer = raw_input("Type left or right and press Enter.") if answer == left: print "The road winds uphill." sleep (5) print "Glass crunches underfoot." sleep (5) print "The trees are all bare." sleep (5) print "The leaves turned to dust." print "They are ground into the cracks, between the paving stones." sleep (5) if answer == right: print "The corridor turns sharply." sleep (2) print "You go deeper inside until you can't see anything" sleep(5) print "You hear a noise from the floor below." sleep (5) print "You can just make out a white doorframe under the staircase." sleep (5) answer2 = raw_input("Do you go into the basement? Type yes, or no.") if answer2 == yes: print "The stairs creak. You fumble along the wall for a lightswitch" sleep (5) print "Do you press it?" if answer3 == yes: print "Light floods the basement. It leaves you blinded for a few seconds..." sleep (5) print "Were you pushed or did you fall?" sleep (2) print "No one remembers that day, or any other. The End" if answer2 == no: print "You can't see a thing. You decide to get out back to the street." sleep (5) print "Night is falling. You are hopelessly lost." sleep (20) sys.exit 

  answer = raw_input("Type left or right and press Enter.") 

если ответ == "left" :

  print "The road winds uphill." sleep (5) print "Glass crunches underfoot." sleep (5) print "The trees are all bare." sleep (5) print "The leaves turned to dust." print "They are ground into the cracks, between the paving stones." sleep (5) 

если answer == "right" :

  print "The corridor turns sharply." sleep (2) print "You go deeper inside until you can't see anything" sleep(5) print "You hear a noise from the floor below." sleep (5) print "You can just make out a white doorframe under the staircase." sleep (5) answer2 = raw_input("Do you go into the basement? Type yes, or no.") 

если answer2 == "yes" :

  print "The stairs creak. You fumble along the wall for a lightswitch" sleep (5) print "Do you press it?" 

если answer3 == "yes" :

  print "Light floods the basement. It leaves you blinded for a few seconds..." sleep (5) print "Were you pushed or did you fall?" sleep (2) print "No one remembers that day, or any other. The End" 

если answer2 == "нет" :

  print "You can't see a thing. You decide to get out back to the street." sleep (5) print "Night is falling. You are hopelessly lost." sleep (20) sys.exit 

вам нужны кавычки, потому что иначе компилятор предполагает, что это переменная, и вы хотите иметь постоянное значение «left», «yes» и т. д., а не переменную, называемую left, yes и т. д.

 if answer == "left": 

вам нужно сравнить строки со строками

 if answer == left: 

пытается сравнить переменный ответ с переменной слева, что python правильно сообщает вам, что нет переменной с именем left

грубой другой возможности было бы определить переменную с именем left наверху

 left = "left" 

(Имейте в виду, что то же самое относится и к вашим другим сравнениям)

Похоже, вы не определили переменные left , right , yes или no . Поскольку ввод представляет собой строку, вы должны сравнить ее со строкой:

 if answer == "left": ... if answer == "right": ... if answer == "yes": ... if answer == "no": ... 

Примечание. Строка в Python заключена в одиночные кавычки 'some string' или двойные кавычки "another string" или, как упоминалось в @JoranBeasley, тройные кавычки """some multi-line string""" .

Вероятно, ваша проблема будет решена следующим образом:

 if answer == "left":