首页 > 解决方案 > 脚本与 C9.io 上的 Codeanywhere 错误工作正常

问题描述

我将工作空间从 C9.io 迁移到 Codeanywhere,在 C9.io 上我的脚本运行良好;我的脚本是我一直在开发的 python 终端游戏/应用程序。我有一个测试脚本来运行我迄今为止创建的功能,以及用于创建不同类和播放器对象的 player.py 文件,包括保存、加载等。在 C9.io 上,这是输出。

Strength: 15
Agility: 10
Critical Chance: 25
Spell Power: 15
Intellect: 5
Speed: 5
---
Health: 100/100
Stamina: 10
Armor: 20
Resilience: 2
---
Strength: 20.25
Agility: 10
Critical Chance: 43.75
Spell Power: 15
Intellect: 5
Speed: 4.0
---
Health: 100/100
Stamina: 7.5
Armor: 16.0
Resilience: 1.4
---
# more test info regarding other scripts

但是当我在 Codeanywhere 上运行测试脚本时,就会发生这种情况。

Strength: 15
Strength: 15
Agility: 10
Critical Chance: 25
Spell Power: 15
Intellect: 5
Speed: 5
---
Health: 100/100
Stamina: 10
Armor: 20
Resilience: 2
---
Traceback (most recent call last):
  File "test.py", line 7, in <module>
    player2 = BerserkerCharacter('Jon', 'Snow')
  File "/home/cabox/workspace/grimside/assets/player.py", line 93, in __init__
    super().__init__()
TypeError: super() takes at least 1 argument (0 given)

我已经尝试在 前面self加上super().__init__()参数self,但我对 Python 不是很熟练,所以我不知道要解决什么问题。这是我的脚本。

测试.py

from assets import *
player = BaseCharacter()
player.show_player_stats("offensive")
print('---')
player.show_player_stats("defensive")
print('---')
player2 = BerserkerCharacter('Jon', 'Snow')
player2.show_player_stats("offensive")
print('---')
player2.show_player_stats("defensive")
print('---')
#more test info regarding other scripts

播放器.py

#import functions used by script
#random is a math function used for creating random integers
import random
#pickle is for saving/loading/writing/reading files
import pickle
#sys is for system-related functions, such as quitting the program
import sys
#create a class called BaseCharacter, aka an Object()
class BaseCharacter:
    #define what to do when the object is created, or when you call player = BaseCharacter()
    def __init__(self):
        #generate all the stats. these are the default stats, not necessarily used by the final class when player starts to play.
        #round(random.randint(25,215) * 2.5) creates a random number between 25 and 215, multiplies it by 2.5, then roudns it to the nearest whole number
        self.gold = round(random.randint(25, 215) * 2.5)
        self.currentHealth = 100
        self.maxHealth = 100
        self.stamina = 10
        self.resil = 2
        self.armor = 20
        self.strength = 15
        self.agility = 10
        self.criticalChance = 25
        self.spellPower = 15
        self.intellect = 5
        self.speed = 5
        self.first_name = 'New'
        self.last_name = 'Player'
        self.desc = "Base Description"
        self.class_ = None
        self.equipment = [None] * 6
    #define the function to update stats when the class is set
    def updateStats(self, attrs, factors):
        #try to do a function
        try:
            #iterate, or go through data
            for attr, fac in zip(attrs, factors):
                val = getattr(self, attr)
                setattr(self, attr, val * fac)
        #except an error with a value given or not existing values
        except:
            raise("Error updating stats.")
    #print out the stats when called
    #adding the category line in between the ( ) makes it require a parameter when called
    def show_player_stats(self, category):
        #if the input for category is put into all upper case and it says "OFFENSIVE", do this
        if category.upper() == "OFFENSIVE":
            #print the stats. {} means a filler, and the .format makes it print the value based off the variables, in order; strength: {} will print strength: 15 if strength = 15
            print("Strength: {}\nAgility: {}\nCritical Chance: {}\nSpell Power: {}\nIntellect: {}\nSpeed: {}".format(self.strength, self.agility, self.criticalChance, self.spellPower, self.intellect, self.speed))
        #or, if the input for category is put into all upper case and it says "DEFENSIVE", do this
        elif category.upper() == "DEFENSIVE":
            #same as before
            print("Health: {}/{}\nStamina: {}\nArmor: {}\nResilience: {}".format(self.currentHealth, self.maxHealth, self.stamina, self.armor, self.resil))
        elif category.upper() == "INFO":
            print("Name: {} {}\nGold: {}\nClass: {}\nClass Description: {}".format(self.first_name, self.last_name, self.gold, self.class_, self.desc))
        #if its anything else
        else:
            #raise an error, formating the Category {} with the category input given
            raise KeyError("Category {} is not a valid category! Please choose Offensive or Defensive.".format(category))
    #save progress to a local file using this function
    def save(self):
        #opening a new file with the filename being put as FirstName_LastName (example New_Player.txt) with read and write permissions as variable f
        with open(self.first_name+'_'+self.last_name, 'wb') as f:
            #use pickle script to dump the information in stats/equipment/etc to a local save file
            pickle.dump(self, f)
    #declare as a static method
    @staticmethod
    #load save file with given name when called
    def load(filename):
        #open the save file with name given as a read only file as variable f
        with open(filename, 'rb') as f:
            #return the stats and update them as needed
            return pickle.load(f)
#classes
#create the warrior class, based off of the base character which automatically uses all the variables, functions, and data from BaseCharacter
class WarriorCharacter(BaseCharacter):
    #define data when initialized, or the Object is created
    def __init__(self, first_name, last_name):
        super().__init__()
        #update the class value since its a copy of BaseCharacter
        self.class_ = 'Warrior'
        #update the first name
        self.first_name = first_name
        #update the last name
        self.last_name = last_name
        #update description value
        self.desc = 'You were  born a protector. You grew up to bear a one-handed weapon and shield, born to prevent harm to others. A warrior is great with health, armor, and defense.'
        #update the values Stamina, Resil, And Armor by multipling current value by 1.25, 1.25, and 1.35.
        self.updateStats(['stamina', 'resil', 'armor', 'strength', 'speed'], [1.25, 1.25, 1.35, 0.75, 0.40])

class BerserkerCharacter(BaseCharacter):
    def __init__(self, first_name, last_name):
        super().__init__()
        self.class_ = 'Berserker'
        self.first_name = first_name
        self.last_name = last_name
        self.desc = 'A natural viking at heart, you are vicious and short-tempered. Your brute strength and anger allows you to dual wield two-handed weapons. Anyone in your way is an obstacle begging to be obliterated. While suffering defensively, you are enhanced offensively.'
        self.updateStats(['stamina', 'armor', 'resil', 'strength', 'criticalChance', 'speed'], [0.75, 0.80, 0.70, 1.35, 1.75, 0.80])

标签: pythonc9.iocodeanywhere

解决方案


根据评论中的讨论:

事实证明,容器(错误地)默认运行 Python 2.7,而不是描述中所述的 3.4。

最简单的解决方法是通过指定参数使调用super对两个版本都有效,例如:

class WarriorCharacter(BaseCharacter):
    #define data when initialized, or the Object is created
    def __init__(self, first_name, last_name):
        super(WarriorCharacter, self).__init__()

class BerserkerCharacter(BaseCharacter):
    def __init__(self, first_name, last_name):
        super(BerserkerCharacter, self).__init__()

推荐阅读