python - 创建时未定义错误选择您自己的冒险
问题描述
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()
解决方案
我按原样运行代码。您从未定义 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()
推荐阅读
- c++ - 复制向量
元素到其他其他向量 *(1作为指针传递) - list - 基于段选择的两个不同实体的列表
- react-native - axios 响应返回对象而不是数组
- collections - 将数据表中的列中的数据添加到 Power Apps 中的集合
- php - 如何对 laravel 雄辩的子关系进行排序并获取有序的父数据
- python - 为什么'django.contrib.admin.sites'没有属性'register'?
- flutter - 快照适用于 Documents[index] 但不适用于 Document(ID)
- javascript - 如何更改传单上缩放按钮的不透明度?
- tesseract - 在使用应用程序图像训练 Tesseract 时需要帮助
- ios - 如何在 SwiftUI 项目中使用 AVAudioPlayer 播放音频