Python错误'对象字符串没有功能'

时间:2015-02-18 15:40:04

标签: python cmd

作为一个相对较新的程序员,我决定将一个小文本冒险作为我的第一个项目。它工作正常,除非你试图去北方。我收到上面的错误发布。我认为这与变量位置有关,但我不确定。

import random
import os
import time
os.system('cls')
yes = ("Yes", "yes", "y", "Y", "Yeah", "yeah", "yea", "Yea", "m8")
no = ("No", "no", "n", "N", "naw", "Naw", "Nope", "nope")
inventory = ("inv", "inventory", "i", "inven", "Inventory")
eats = ("eat","e", "Eat")
statss = ("stats", "s", "statistics", "Stats")
attack = ("attack", "combat", "a", "c", "fight", "Attack", "Combat", "Fight")
use = ("use", "u", "Use")
northh = ("Go North", "go north", "north", "North", "N", "n")
food = ("Bread")
items = ("Lantern")
weapons = ("Dagger")
hunger = 0
gold = 0
hp = 0 
inv = []
class Room:
    def __init__(self, name, desc, items, north):
        self.name = name
        self.desc = desc
        self.items = items
        self.north = north
CaveEntrance = Room("CaveEntrance", "The entrance to a large cave.", "Lantern", "None")
Beach = Room("Beach", "The beach. The sand is warm and the water is inviting.", "Lantern", "CaveEntrance")
inv.append ("Bread")
print("Hello, adventurer. What is your name?")
name = input("> ") 
print("Hello,",name,".",)
print("You have just begun a grand adventure. Prepare yourself now.")
def gen():
    global hp
    global gold      
    global hunger
    hp = random.randint(10, 25)
    gold = random.randint(0, 200)
    hunger = random.randint(10, 50)
    print("You start your journey with", hp, "hit points and", gold, "gold. You also have",hunger,"food.")
    print("Is this correct?")
    print("Y/N")
    ans = input("> ")
    if ans in yes:
        print("We will now continue") 
    if ans in no:
        print("Let's try again.")   
        gen()
gen()
print("STORY:")
print("You are going on vacation to the beach with your parents. You play in the waves for some time before noticing something off in the distance. Your curiosity is piqued. ")
location = Beach
def eat():
    global hunger
    print(*inv,sep='\n')
    print("Eat which item?")
    eatitem = input("> ")
    if eatitem in food and eatitem in inv:
        hunger = hunger + 5
        inv.remove(eatitem)
        print("Yum.")
        cmd()     
    elif eatitem not in inv:
        print("You don't have",eatitem,".")
        cmd()
    elif eatitem not in food:
        print("You can't eat that!")
        cmd()
def north():
    global location
    print("Heading north towards", location.north,"...") 
    time.sleep(2)
    location = location.north
    cmd()
def stats():
    global hp
    global hunger
    global gold
    print("HitPoints:",hp)
    print("Food:",hunger)
    print("Gold:",gold)
    cmd ()
def cmd():
    global location
    global hunger
    print()
    print(location.name)
    print()
    print(location.desc)
    print()
    hunger = hunger - 1
    urcmd = input("> ")
    if urcmd in northh:
        north()
    elif urcmd in inventory:
        if not inv:
            print("Inventory empty.")
        else:
            print(*inv,sep='\n')
        cmd()
    elif urcmd in eats:
        eat()
    elif urcmd in statss:
        stats()
    elif urcmd in attack:
        combat()
    elif urcmd in use:
        use()
    elif urcmd == ("exit"):
        print("Are you sure you want to quit?")
        print()
        print("All progress will be lost!")
        print()
        print("Y/N")
        ans = input("> ")
        if ans in yes:
            print("Ok, exiting...")
            exit()
        else:
            print("Ok, returning to game.")
            cmd()
    else:
        print("That is not a command. For a list of commands, type help.")
        cmd()
cmd()

1 个答案:

答案 0 :(得分:3)

问题是当你第一次去北方时,你的当前位置被设置为字符串"CaveEntrance"而不是CaveEntrance对象。

更改此行:

Beach = Room("Beach", "The beach. The sand is warm and the water is inviting.",
             "Lantern", "CaveEntrance")

要:

Beach = Room("Beach", "The beach. The sand is warm and the water is inviting.",
             "Lantern", CaveEntrance)