自学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
为什么显示该参数未定义