小编use*_*742的帖子

什么时候最好在Python中使用一个类?

我是python和编程的新手,所以非常感谢你对这一点的任何澄清.

例如,在以下代码中:

    #Using a class
class Monster(object): 
    def __init__(self, level, damage, duration): 
        print self
        self.level = level
        self.damage = damage
        self.duration = duration

    def fight(self):
        print self 
        print "The monster's level is ", self.level
        print "The monster's damage is ", self.damage
        print "The attack duration is ", self.duration

    def sleep(self): 
        print "The monster is tired and decides to rest."

x = Monster(1, 2, 3)
y = Monster(2, 3, 4)
x.fight()
y.fight()
    y.sleep() 

   #just using a function
def fight_func(level, damage, duration): …
Run Code Online (Sandbox Code Playgroud)

python oop class function

8
推荐指数
1
解决办法
295
查看次数

如何删除字符串后面的所有标点符号?

这是一个用户可以输入像"冰茶"这样的值的游戏.我想操纵字符串返回"冰茶"而没有拖尾标点符号.

寻找最优雅/最简单的python解决方案.

试着

def last_character(word):
  if word.endswith('.' or ','):
      word = word[:-1]
  return word 
Run Code Online (Sandbox Code Playgroud)

如果最后只有一个标点符号,则有效.但它并非包罗万象.

找到了Java解决方案:

String resultString = subjectString.replaceAll("([a-z]+)[?:!.,;]*", "$1");
Run Code Online (Sandbox Code Playgroud)

python regex string

7
推荐指数
1
解决办法
8060
查看次数

Python TypeError:'str'对象不能为类调用

请帮我理解这个.我创建了一个非常简单的程序来尝试理解类.

class One(object):
    def __init__(self, class2):
        self.name = 'Amy'
        self.age = 21
        self.class2 = class2

    def greeting(self):
        self.name = raw_input("What is your name?: ")
        print 'hi %s' % self.name

    def birthday(self):
        self.age = int(raw_input("What is your age?: "))
        print self.age 

    def buy(self):
        print 'You buy ', self.class2.name

class Two(object):
    def __init__(self): 
        self.name = 'Polly'
        self.gender = 'female'

    def name(self):
        self.gender = raw_input("Is she male or female? ")
        if self.gender == 'male'.lower():
            self.gender = 'male'
        else:
            self.gender = 'female'

        self.name = …
Run Code Online (Sandbox Code Playgroud)

python class

7
推荐指数
1
解决办法
6053
查看次数

Python类继承AttributeError - 为什么?怎么修?

关于SO的类似问题包括:这个这个.我还阅读了我能找到的所有在线文档,但我仍然很困惑.我很感激你的帮助.

我想在CastSpell类lumus方法中使用Wand类.wandtype属性.但我不断收到错误"AttributeError:'CastSpell'对象没有属性'wandtype'."

此代码有效:

class Wand(object):
    def __init__(self, wandtype, length):
        self.length = length 
        self.wandtype = wandtype

    def fulldesc(self):
        print "This is a %s wand and it is a %s long" % (self.wandtype, self.length) 

class CastSpell(object):
    def __init__(self, spell, thing):
        self.spell = spell 
        self.thing = thing

    def lumus(self):
        print "You cast the spell %s with your wand at %s" %(self.spell, self.thing) 

    def wingardium_leviosa(self): 
        print "You cast the levitation spell."

my_wand = Wand('Phoenix-feather', '12 inches') 
cast_spell = CastSpell('lumus', 'door') …
Run Code Online (Sandbox Code Playgroud)

python inheritance class

6
推荐指数
1
解决办法
6057
查看次数

在终端中运行python脚本,没有打印或显示 - 为什么?

通过艰难的方式学习Python,第25课.

我尝试执行脚本,结果如​​下:

myComp:lphw becca$ python l25 

myComp:lphw becca$ 
Run Code Online (Sandbox Code Playgroud)

终端中没有打印或显示任何内容.

这是代码.

def breaks_words(stuff): 
    """This function will break up words for us."""
    words = stuff.split(' ')
    return words 

def sort_words(words):
    """Sorts the words."""
    return sorted(words)

def print_first_word(words):
    """Prints the first word after popping it off."""
    word = words.pop(0)
    print word

def print_last_word(words):
    """Prints the last word after popping it off."""
    word = words.pop(-1)
    print word

def sort_sentence(sentence): 
"""Takes in a full sentence and returns the sorted words."""
    words = break_words(sentence)
    return sort_words(words) …
Run Code Online (Sandbox Code Playgroud)

python terminal

3
推荐指数
1
解决办法
2万
查看次数

如何仅在图像悬停时显示fancybox标题

我正在使用Fancybox插件作为图片库,我只想在用户将鼠标悬停在图片上时显示图片标题.我无法弄清楚要修改的代码部分是为了实现这一点.

我已经尝试通过向a :hover以下内容添加状态来编辑CSS :

.fancybox-title-over-wrap {
}
Run Code Online (Sandbox Code Playgroud)

我甚至试过在这里玩CSS可见性设置:

.fancybox-opened .fancybox-title {
}
Run Code Online (Sandbox Code Playgroud)

但是,我没有运气.

我必须在实际的JS文件中更改一些内容吗?任何帮助将非常感谢!

jquery modal-dialog gallery fancybox

1
推荐指数
1
解决办法
1万
查看次数