【问题标题】:Modifying a function from another in python在python中从另一个函数修改一个函数
【发布时间】:2012-06-05 20:12:16
【问题描述】:

对于python的在线课程,我正在用python制作一个基本的基于文​​本的冒险游戏。

现在,我有一个基本的库存系统,它通过布尔值来判断用户是否有物品,以及有限物品的整数,例如弹药和诸如此类的东西。 这是库存系统的代码

def Inventory(self): #The inventory for the game.  I don't know how to program it properly, so this is my testing ground. 
#This will hold the boolean values to if the player has the items or not.  Another will be used to show the user the items

    street_clothes = False
    pistol = False
    ammo = 0
    phone = False

这是我试图修改上面的库存功能的代码

#Eric's apartment
 def Eric_Apartment(self):

    print "type in grab your gun"

    action = raw_input("> ")

    if action == "grab":
        self.Inventory(CR97) = True
    #   self.CR97_ammo += 15
    #   print CR97_ammo
    #   print self.CR97_ammo

    exit(1)

尝试运行此程序时出现此错误:

python ex43.py
File "ex43.py", line 78
self.Inventory(CR97) = True
SyntaxError: can't assign to function call

还有什么我应该做的吗?我对python很陌生,这是我自己的第一个项目。

这里是完整代码,供参考

from sys import exit #allows the program to use the exit(1) code
from random import randint #allows the program to use a random number

class Game(object):

#quotes that pop up if the person dies, and also defines the start and self variables
 def __init__(self, start):
    self.quips = [
        "You lose!"
    ]
    self.start = start

 def Inventory(self): #The inventory for the game.  
#This will hold the boolean values to if the player has the items or not. 

    street_clothes = False
    pistol = False
    ammo = 0
    phone = False


#this function launches the game, and helps with the room transfer
 def play(self):
    next = self.start

    while True:
        print "\n---------"
        room = getattr(self, next)
        next = room( )

#if the user dies, or fails at the game, this is the function that is ran
 def death(self):
    print self.quips[randint(0, len(self.quips)-1)]
    exit(1)

#Welcome screen to the game
 def welcome_screen(self):
    print " place holder"
    return 'intro_screen'

#Intro screen to the game
 def intro_screen(self):

    print "place holder"

    action = raw_input("> Press any key to continue ")

    return 'Eric_Apartment'

#Eric's apartment
 def Eric_Apartment(self):

    print "type in grab your gun"

    action = raw_input("> ")

    if action == "grab":
        self.Inventory(CR97) = True
    #   self.CR97_ammo += 15
    #   print CR97_ammo
    #   print self.CR97_ammo

    exit(1)

a_game = Game("welcome_screen")
a_game.play()

【问题讨论】:

    标签: python function boolean


    【解决方案1】:

    这是一种非常反常的做法。为什么要使用函数来存储数据?

    只要有一个玩家对象,带有一个库存数组。

    我也建议使用对象来建模项目。很好地用于类层次结构。可以有一个基础项,具有子类 SingleItem 和 StackableItem 等。

    【讨论】:

    • 我建议使用set 来表示库存
    • 我对 OOP 很陌生。我是否会修改另一个类中的内容,比如我让玩家拿枪:codeInventory.gun = True code 假设我仍然使用布尔值来存储玩家是否拥有它?
    • 没关系,我测试了那部分。有用。我做过的唯一 OOP 编程是 Java。我还是个新手。
    【解决方案2】:

    不要使用函数,而是尝试使用类-

    class Player:
        def __init__(self):
            self.street_clothes = False
            self.pistol = False
            self.ammo = 0
            self.phone = False
        def give_street_clothes(self):
            self.street_clothes = True
        # etc
    

    但就个人而言,我不会使用每个项目作为布尔值,而是使用项目列表:

    class Player:
        def __init__(self):
            self.inventory = []
            # add code for ammo/pistol
        def has_item(self, item):
            return item in self.inventory
        def give_item(self, item):
            self.inventory.add(item)
        def remove_item(self, item):
            self.inventory.remove(item)
        # etc
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-11
      • 2017-01-24
      • 2020-04-01
      • 1970-01-01
      • 2012-05-03
      相关资源
      最近更新 更多