无论我尝试了什么,似乎都无法消除错误消息。来自 "Hello! Python" 本书

Can't seem to rid of the error message no matter what I've tried. From "Hello! Python" book

这是我在尝试 运行 下面的代码时遇到的错误:

Traceback (most recent call last):
  File "/Users/JPagz95/Documents/Hunt_the_Wumpus_3.py", line 76, in <module>
    visit_cave(0)
  File "/Users/JPagz95/Documents/Hunt_the_Wumpus_3.py", line 15, in visit_cave
    unvisited_caves.remove(cave_number)
AttributeError: 'range' object has no attribute 'remove'

我知道 int 和 range 没有这些功能,但它们最初不应该这样定义。我意识到这是很多代码,但我觉得问题可能出在不止一个地方。如果有帮助,此代码基于 "Hello! Python" 书中的第 to 章。如果能得到任何帮助,我将不胜感激!

from random import choice

cave_numbers = range(0,20)
unvisited_caves = range(0,20)
visited_caves = []

def create_tunnel(cave_from, cave_to):
    """ create a tunnel between cave_from and cave_to """
    caves[cave_from].append(cave_to)
    caves[cave_to].append(cave_from)

def visit_cave(cave_number):
    """ mark a cave as visited """
    visited_caves.append(cave_number)
    unvisited_caves.remove(cave_number)

def choose_cave(cave_list):
    """ pick a cave from a list, provided that the
    cave has less than 3 tunnels """
    cave_number = choice(cave_list)
    while len(caves[cave_number]) >= 3:
        cave_number = choice(cave_list)
    return cave_number

def print_caves():
    """ print out the current cave structure """
    for number in cave_numbers:
        print (number, ":", caves[number])
    print ('----------')

def setup_caves(cave_numbers):
    """ create a starting list of caves """
    caves = []
    for number in cave_numbers:
        caves.append([number])
    return caves

def link_caves():
    """ make sure all of the caves are connected with two way tunnels"""
    while unvisited_caves != []:
        this_cave = choose_cave(visited_caves)
        next_cave = choose_cave(unvisited_caves)
    create_tunnel(this_cave, next_cave)
    visit_cave(next_cave)

def finish_caves():
    """ link the rest of the caves with one way tunnels """
    for cave in cave_numbers:
        while len(caves[cave]) < 3:
            passage_to = choose_cave(cave_numbers)
            caves[cave].append(passage_to)

def print_location(player_location):
    """ tell the player about where they are """
    print ("You are in a cave ", player_location)
    print ("From here you can see caves: ")
    print (caves[player_location])
    if wumpus_location in caves[player_location]:
        print ("I smell a wumpus!")

def get_next_location():
    """ Get the player's next location """
    print ("Which cave next?")
    player_input = input(">")
    if (not player_input.isdigit() or
        int(player_input) not in
            caves[player_location]):
        print(player_input + "?")
        print ("That's not a direction I can see!")
        return none
    else:
        return int(player_input)

caves = setup_caves(cave_numbers)

visit_cave(0)
print_caves()
link_caves()
print_caves()
finish_caves()

wumpus_location = choice(cave_numbers)
player_location = choice(cave_numbers)
while player_location == wumpus_location:
    player_location = choice(cave_numbers)

print ("Welcome to Hunt the Wumpus!")
print ("You can see ", len(cave_numbers), " caves")
print ("To play, just type the number")
print ("of the cave you wish to enter next")

while True:
    print_location(player_location)
    new_location = get_next_location()
    if new_location != None:
        player_location = new_location
    if player_location == wumpus_location:
        print ("Aargh! You got eaten by a wumpus!")

确保声明为

unvisited_caves = list(range(0,20))

默认range(0,20) returns一个"generates"值0到20的对象。它效率更高,因为它只需要记住开始和结束值(0和 20) 沿步长(在您的情况下为 1)。

将此与必须存储从 0 到 20 的每个值的 列表 对比,您可以从此列表中删除单个值。