2016-12-03 5 views
0

私のメソッドincrement4が機能しない理由を理解しようとしています。これらのメソッドはすべて機能としてはうまく機能しましたが、今では機能していないメソッドに変換しました。私は "時間"のオブジェクト名を "自己"に置き換えました。私は "リターン"の維持と削除を実験しました。 EDIT親切に私に指摘されたので、私はドット表記法を使用していませんでした。私はいくつかの変更を加えました。今、Pythonは私に別のエラーを与えている:Pythonでクラスにメソッドを追加すると、エラー:グローバル名が定義されていません

Traceback (most recent call last): 
    File "/Users//Desktop/temp.py", line 33, in <module> 
    currenttime.increment4(4000) 
    File "/Users//Desktop/temp.py", line 22, in increment4 
    newtime = float(total_seconds).make_time() 
AttributeError: 'float' object has no attribute 'make_time' 
>>> 

ライン33は以下のとおりです。

currenttime.increment4(4000) 

ライン22は以下のとおりです。

class Time: 
    def printTime(self): 
     print str(time.hours)+":"+str(time.minutes)+":"+str(time.seconds) 

    def make_time (self): 
     time = Time() 
     time.hours = self/3600 
     time.minutes = (self % 3600)/60 
     time.seconds = (self % 3600) % 60 
     return time 

    def covertTOseconds (self): 
     hours = self.hours * 3600 
     minutes = self.minutes * 60 
     seconds = self.seconds 
     amt_in_seconds = hours + minutes + seconds 
     return amt_in_seconds 

    def increment4 (self, increaseINseconds): 
     total_seconds = self.covertTOseconds() + increaseINseconds 
     newtime = float(total_seconds).make_time() 
     newtime.printTime() 


currenttime = Time() 
currenttime.hours = 3 
currenttime.minutes = 47 
currenttime.seconds = 45 



currenttime.increment4(4000) 

newtime = float(total_seconds).make_time() 

そしてここでは、全体のことです

+1

'self.covertTOseconds()'、及びPEP-8( 'convert_to_seconds')あたりの命名規則を修正。 – jonrsharpe

+0

ありがとうジョン!私はあなたの提案を実装し、私は自分の質問を編集しています。それは今、別のエラーを投げている。 –

+0

エラーメッセージは、問題の内容を正確に伝えます。 – jonrsharpe

答えて

0

コードを少し変更する必要があります:

class Time: 
    def printTime(self, time): 
     print str(time.hours)+":"+str(time.minutes)+":"+str(time.seconds) 

    def increment4(self, increaseINseconds): 
     seconds = self.covertTOseconds() + increaseINseconds 
     time = self.makeTime (seconds) 
     print time 

    def makeTime(self, totalseconds): 
     time = Time() 
     time.hours = totalseconds/3600 
     time.minutes = (totalseconds % 3600)/60 
     time.seconds = (totalseconds % 3600) % 60 
     return time 

    def covertTOseconds(self): 
     hours = self.hours * 3600 
     minutes = self.minutes * 60 
     seconds = self.seconds 
     totalseconds = hours + minutes + seconds 
     return totalseconds 

エラーはそれがselfと呼ばれるべきですのでconverTOsecondsが定義されていない示しているように、また、それは引数を取りません。したがって、covertTOseconds (self)からself.convertTOseconds()に変更する必要があります。お役に立てれば。

あなたが質問を更新したので、何をやりたいかもしれませんが、このようなクラスを持っているように私には思える:

class Time: 
    def __init__(self): 
     self.hours = None 
     self.minutes = None 
     self.seconds = None 
     self.total_seconds = None 

    def to_seconds(self): 
     hours = self.hours * 3600 
     return (self.hours * 3600) + (self.minutes * 60) + self.seconds 

    def increment(self, x): 
     self.total_seconds = self.to_seconds() + x 
     self.hours = self.total_seconds/3600 
     self.minutes = (self.total_seconds % 3600)/60 
     self.seconds = (self.total_seconds % 3600) % 60 
     self.__str__() 

    def __str__(self): 
     print("{0}:{1}:{2}".format(self.hours, self.minutes, self.seconds)) 

次のように、あなたはそのクラスを使用することができます。

c_time = Time() 
c_time.hours = 3 
c_time.minutes = 47 
c_time.seconds = 45 

c_time.increment(4000) 

この出力:

4:54:25 
+0

ホセありがとう。私はあなたの提案に基づいていくつかの編集を行いました。 AttributeError: 'int'オブジェクトに 'makeTime'属性がありません。私はここに欠けている概念が何であるか分かりません... –

+0

整数型の 'total_seconds'に' makeTime'メソッドを呼び出そうとする 'increment4'メソッドを修正しました。 –

+0

申し訳ありません私はあなたの編集を見た前にこれを書きました!私は別の質問をする前にそれらを試してみます。ありがとう! –

関連する問題