我可以在if语句中使用if语句

时间:2015-08-27 14:27:56

标签: if-statement

是否可以在if语句中使用if语句? 例如:

If answer=10:
     answer2=input("Do you agree")
     if answer2=yes
         print("You agree")
     else: 
         print("You disagree")


else:
print("You don't answer")

1 个答案:

答案 0 :(得分:0)

对于if语句中if语句的问题,您将使用三元运算:

class Scene(object):

    def enter(self):
        pass

class Desert(Scene):

    def enter(self, hero):
        print "You're in the desert after the plane crashed. Choose a path. You can go west to the plane, south to the village Waikiki and east to the dungeon."
        action = raw_input("[east, west, south]>")
        if action == "east":
            return Dungeon()
        elif action == "west":
            return Plane()
        elif action == "south":
            return Waikiki()
        elif action == "north":
            return Room("Goldroom", "Look out for monsters!")

class Plane(Scene):
    def enter(self, hero):
        print "This is the plane you used to come to this place. It is completely broken."
        actions = ['investigate', 'return']
        action = ""
        while action not in actions: 
            action = raw_input("[investigate, return] >")
        if (action == actions[0]):
            print "You investigate the plane and you find a health-care package"
            hero.obtain_item("health-care package")
            return self
        elif (action == actions[1]):
            return Desert()



class Dungeon(Scene):

    def enter(self, hero):
        print "You are in the dungeon. Lots of monsters are here. Take care!"

        for i in range(0,3):
            print "There is a huge bat!"
            battle = Battle("bat", i+1, hero)
            outcome = battle.fight()
        if outcome:
            return Dungeon_b()
        else:
            return Death()

class Dungeon_b(Scene):

    def enter(self, hero):
        print "this is great, you won the game"
        return Win()

class Waikiki(Scene):

    def enter(self, hero): 
        print hero.name + " goes to the village leader. He greats you and let you go. But than an arrow comes from far and kills you."
        return Death()

class Death(Scene):
    def enter(self, hero):
        print "You died. Try again."
        exit(1)

class Win(Scene):
    def enter(self, hero):
        print "You brought " + hero.name + " to the finish. Great!"
        exit(1)

class Room(object):

    def __init__(self, name, description):
        self.name = name
        self.description = description
        self.paths = {}

    def go(self, direction):
        return self.paths.get(direction, None)

    def add_paths(self, paths):
        self.paths.update(paths)

central_corridor = Room("Central Corridor", "description about central corridor")
laser_weapon_armory = Room("Laser Weapon Armory", "description about laser weapon armory")
the_bridge = Room("The Bridge", "description of the bridge")
escape_pod = Room("Escape Pod", "description of the escape pod")
the_end_winner = Room("The End", "description of the end scene")
the_end_loser = Room("The End", "description of the end scene when the player died.")

escape_pod.add_paths({
    '2': the_end_winner,
    '*': the_end_loser
    })

generic_death = Room('death', 'You died')

the_bridge.add_paths({
    'throw the bomb': generic_death,
    'slowly place the bomb': escape_pod
    })

laser_weapon_armory.add_paths({
    '0132': the_bridge,
    '*': generic_death
    })

central_corridor.add_paths({
    'shoot!': generic_death,
    'dodge!': generic_death,
    'tell a joke': laser_weapon_armory })

START = central_corridor

但我认为你的问题错了。在您的示例中,如果answer = 10,您希望提示用户输入某些内容,然后根据其响应执行不同的操作。很难真正告诉你如何在不知道你正在编写什么语言的情况下实现它,但是大多数语言都有某种方式来创建“代码块”,通常是这样的:

if((condition1) ? condition2 : condition3)
   code to execute

虽然有些语言只是我们缩进