脚本与 C9.io 上的 Codeanywhere 错误工作正常

Posted

技术标签:

【中文标题】脚本与 C9.io 上的 Codeanywhere 错误工作正常【英文标题】:Codeanywhere error on script vs C9.io working fine 【发布时间】:2018-10-25 09:12:21 【问题描述】:

我将我的工作区从 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 不是很熟练,所以我不知道要解决什么问题。这是我的脚本。

test.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

player.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])

【问题讨论】:

看起来 Codeanywhere 正在运行 Python 2.7? @UnholySheep 我确保选择了 python 3 容器,这很奇怪.. 生病仔细检查 @UnholySheep drive.google.com/file/d/18QXmMvQoXRaLlKKKgMzke0kOkOp5r1ei/… 它正在运行 3.4 只是为了 100% 确定,你能从 Python 中检查它吗? (例如:import sys; print(sys.version) 【参考方案1】:

根据cmets中的讨论:

事实证明,容器(错误地)默认运行 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__()

【讨论】:

以上是关于脚本与 C9.io 上的 Codeanywhere 错误工作正常的主要内容,如果未能解决你的问题,请参考以下文章

如何在 c9.io 上创建数据库

如何从 Codeanywhere 查看 PHP 错误日志?

推荐一个WebIDE在线编程语言编译器C9.io

意外 ctrl + Z 后 localhost 超时

在 CodeAnywhere 上安装 Phalcon

为啥 Codeanywhere 容器无法打开并一直显示“正在恢复”?