里奥文 2019-01-16 15:06 采纳率: 50%
浏览 434
已采纳

求助!为什么显示“room_num”未定义?

自学python,跟着书本做了个例子,想要在里面实现一个后退backwards()的功能,定义了globe roomnum 但是运行报错显示未定义

from sys import exit
global room_num

def start():
    print("You are in a dark room.")
    print("There is a door to your right and left.")
    print("Which one do you take?")
    room_num = 0

    choice = input(">> ")

    if choice == "left":
        bear_room()
    elif choice == "right":
        cthulhu_room()
    else:
        dead("You are stumble around the room until you starve.")

def bear_room():
    print("There is a bear here.")
    print("The bear has a bunch of honey.")
    print("The fat bear is in front of another door.")
    print("How are you going to move the bear?")
    bear_moved = False
    room_num = 1

    while True:
        choice = input(">> ")

        if choice == "take honey":
            dead("The bear looks at you then slaps your face off.")
        elif choice == "taunt bear" and not bear_moved:
            print("The bear has moved from the door.")
            print("You can go through it now.")
            bear_moved = True
            room_num = 2

            choice = input("input again>>")

            if choice == "open door" and bear_moved:
                gold_room()
            elif choice == "back":
                backwards()
            else:
                dead("You caught bear's eye and died.")

        elif choice == "taunt bear" and bear_moved:
            dead("The baer gets pissed off and chews your leg off.")
        elif choice == "back":
            backwards()
        else:
            dead("I got no idea what that means.")

def cthulhu_room():
    print("Here you see the great evil Cthulhu.")
    print("He, it, whatever stares at you and you go insane.")
    print("Do you flee for your life or eat your head?")
    room_num = 1

    choice = input(">> ")

    if "flee" in choice:
        start()
    elif "head" in choice:
        dead("Well that was tasty!")
    elif "back" in choice:
        backwards()
    else:
        print("I got no idea what that means.")
        print("\n")
        cthulhu_room()

def gold_room():
    print("This room is full of gold.   How much do you take?")
    room_num = 3
    print(f"The current room number is {room_num}")

    choice = input('>> ')
    if choice.isdigit():
        how_much = int(choice)

        if how_much < 50:
            print("Nice, you're not greedy! You win!")
            exit(0)
        else:
            dead("You are greedy bastard!")

    elif choice == "back":
        backwards()
    else:
        dead("Man, learn to type a number.")

def dead(why):
    print(why, "You lose!")
    exit(0)

def backwards():
    if room_num == 1:
        start()
    elif room_num == 2:
        bear_room()
    elif room_num == 3:
        bear_room()
    else:
        start()**

start()

报错
Traceback (most recent call last):
File "ex35.py", line 111, in
start()
File "ex35.py", line 14, in start
bear_room()
File "ex35.py", line 53, in bear_room
backwards()
File "ex35.py", line 102, in backwards
if room_num == 1:
NameError: name 'room_num' is not defined
为什么显示该参数未定义

  • 写回答

1条回答 默认 最新

  • 里奥文 2019-01-16 15:25
    关注

    给backwards一个参数r_num

    def backwards(r_num):
        if r_num == 1:
            start()
        elif r_num == 2:
            bear_room()
        elif r_num == 3:
            bear_room()
        else:
            start()
    

    即可

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助