首页 > 解决方案 > 创建时未定义错误选择您自己的冒险

问题描述

def game():
    print("You wake up in a field. Alone. You wander around and find a small shed with a large, steel padlock on it. \n Around the shed, you find three items. You find: \n A) A shovel \n B) A vial of Nitric Acid \n C) A crowbar \n Which item do you choose to utilize? (A, B, or C)")
    scene1 = input().upper()
    if scene1 == 'A':
        print("You dig a hole. You keep digging until you find yourself stuck. \n You struggle to find a way out when suddenly... \n your shovel hits something hard. You dig it up and you find a toolbox - it looks like it\'s from the World War Three era. \n You open it and find: \n A) A stick of dynamite \n B) Bird feed \n C) A rope ladder \n Which item do you choose (A, B, or C)?")
    if scene1 == 'B':
        print("You carefully pick up the vial of Nitric Acid and carry it to the padlock. \nSuddenly, a strong gust of wind blows the vial out of your hands where it breaks and splashes on you. \nYou are now suffering from third-degree chemical burns. You die... Do you want to play again? (YES or NO)")
        endgame = input()
while endgame == 'yes':
    game()

标签: python

解决方案


我按原样运行代码。您从未定义 endgame 变量,因此 while 循环在第一次尝试运行时会失败。

在循环开始之前设置变量并确保使用全局 endgame 变量:

endgame = 'yes'
def game():
    global endgame 
    print("You wake up in a field. Alone. You wander around and find a small shed with a large, steel padlock on it. \n Around the shed, you find three items. You find: \n A) A shovel \n B) A vial of Nitric Acid \n C) A crowbar \n Which item do you choose to utilize? (A, B, or C)")
    scene1 = input().upper()
    if scene1 == 'A':
        print("You dig a hole. You keep digging until you find yourself stuck. \n You struggle to find a way out when suddenly... \n your shovel hits something hard. You dig it up and you find a toolbox - it looks like it\'s from the World War Three era. \n You open it and find: \n A) A stick of dynamite \n B) Bird feed \n C) A rope ladder \n Which item do you choose (A, B, or C)?")
    if scene1 == 'B':
        print("You carefully pick up the vial of Nitric Acid and carry it to the padlock. \nSuddenly, a strong gust of wind blows the vial out of your hands where it breaks and splashes on you. \nYou are now suffering from third-degree chemical burns. You die... Do you want to play again? (YES or NO)")
        endgame = input()
while endgame.lower() == 'yes':
    game()

推荐阅读