条件码在我的Python代码中不起作用

时间:2014-05-19 17:00:08

标签: python if-statement conditional

这是一次简单的文字冒险尝试。我无法设法使条件按照我希望的方式工作。当给出用户输入时,例如' 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 %s o 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

4 个答案:

答案 0 :(得分:6)

if answer == "left":

您需要将字符串与字符串进行比较

if answer == left:

试图将变量answer与变量left进行比较,python正确告诉你没有名为left的变量

粗略的另一种可能性是在顶部定义一个名为left的变量

left = "left"

(请记住,这同样适用于您的其他比较)

答案 1 :(得分:2)

您似乎尚未定义变量leftrightyesno。由于输入是一个字符串,您应该将它与字符串进行比较:

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

注意: Python中的字符串用单引号'some string'或双引号"another string"括起来,或者如@JoranBeasley所提到的那样,用三引号"""some multi-line string"""括起来。

答案 2 :(得分:0)

您的问题可能会通过以下方式解决:

if answer == "left":

答案 3 :(得分:0)

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

如果回答== "离开"

     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)

如果回答== "对"

     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 == "是"

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

如果answer3 == "是"

     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等的变量