Python错误“object string has no function”

2024-09-29 23:21:20 发布

您现在位置:Python中文网/ 问答频道 /正文

作为一个相对较新的程序员,我决定做一个小的文本冒险作为我的第一个项目。它工作得很好,除非你想去北方。我收到上面的错误帖子。我相信这和可变位置有关,但我不确定。你知道吗

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()

Tags: nameincmdyoulocationglobalhpprint
1条回答
网友
1楼 · 发布于 2024-09-29 23:21:20

问题是,当您第一次北上时,您的当前位置设置为字符串"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)

相关问题 更多 >

    热门问题