2016-10-05 5 views
-2

私はPythonには本当に新しく、私はテレビシミュレータを作る必要があります。私は基本的にネット全体を検索しましたが、本当に私の答えを見つけることはできません。私の問題は、ファイルの状態をファイルに保存しなければならないということです。プログラムを再度開くと、以前の状態(channel、show、volume)を取得する必要があります。私はpickleを使ってファイルを保存しますが、以前の状態は取得しません。Tvシミュレータを作る - Python

うまくいけば、私は明確に明確にするために私に尋ねるために自由に感じることができればうまくいけば十分です。

は、読書のための事前

グローバル変数を使用して
class Television(object): 
    currentState = [] 

    def __init__(self,volume,channel,show): 
     self.volume=volume 
     self.channel=channel 
     self.show=show 

    def changeChannel(self,choice): 

     self.channel=choice 
     return self.channel 

    def increaseVolume(self,volume): 
     if volume in range(0,9): 
      self.volume+=1 
     return self.volume 

    def decreaseVolume(self,volume): 
     if volume in range(1,10):    
      self.volume-=1   
     return self.volume 

    #def __str__(self): 
     #return "[channel: %s show: %s,volume: %s]" % (self.channel, self.show, self.volume) 
    def __str__(self): 
     return "Channel: "+str(self.channel)+\ 
       "\nShow: "+ str(self.show)+\ 
       "\nVolume: "+ str(self.volume) 
           #printing object, state = "print(object)" 
    def getState(self):   
     return self.volume 

    def setState (self,channel,show): 
     self.volume=5 
     self.channel=channel[1] 
     self.show=show[1] 


##################################################################### 

from tvsimulator import* 

import pickle, os.path 

#from builtins import int 

channel = ["1. Mtv","2. Tv 3","2. Svt","4. Kanal4"] 

show = ["Music is life", "Har du tur i karlek?", "Pengar ar inte allt","Vem vill inte bli miljonar"] 

volume = 5 

global myTv 

livingRoomTv = Television(channel,show,volume) 

kitchenTv = Television(channel,show,volume) 




def saveFile(): 

    with open("tvState.pkl",'wb') as outfile: 

     pickle.dump(livingRoomTv,outfile) 
     pickle.dump(kitchenTv,outfile) 

def importFile():  

    with open("tvState.pkl",'rb') as infile: 

     livingRoomTv = pickle.load(infile) 
     kitchenTv = pickle.load(infile) 


def channelMenu(myTv):  
    for i in channel: 
     print(i)     
    choice =int(input(print("Which channel do you want?"))) 
    choice = channel[choice-1]      
    myTv.changeChannel(choice) 
    selection =myTv.changeChannel(choice) 
    return selection 


def methodSelection(myTv): 

    print("1: Change channel") 
    print("2: Volume Up") 
    print("3: Volume Down") 
    print("4: Return to main menu")  
    choice = int(input(print("\nPleas make a selection from the above list\n"))) 
    print(myTv) 
    try:      
     if choice ==1: 
      channelMenu(myTv) 
      print(myTv)   
      methodSelection(myTv) 
     if choice ==2: 
      myTv.increaseVolume(volume) 
      print(myTv) 
      methodSelection(myTv) 
     if choice ==3: 
      myTv.decreaseVolume(volume) 
      print(myTv) 
      methodSelection(myTv) 
     if choice ==4: 
      mainMenu() 
    except: 
     print("Wrong selection, please try again") 




def mainMenu(): 

    print("1: Livingroom Tv") 
    print("2: Kitchen TV") 
    print("3: Exit\n")  
    choice = int(input(print("Please make a selection from the above list")))   
    try:    
     if choice == 1: 
      print("Living room\n")  
      print(livingRoomTv) 
      myTv = livingRoomTv   
      methodSelection(myTv)  
     if choice == 2: 
      print("Kitchen\n")  
      print(kitchenTv) 
      myTv=kitchenTv   
      methodSelection(myTv)   
     if choice == 3: 
      saveFile() 
      print("Tv Shut Down") 
      exit 
    except: 
     print("Wrong selection, please try again") 



def startUp(): 

    if os.path.isfile("tvState.pkl"): 
     print("Tv restored to previous state") 
     importFile() 
     kitchenTv.getState() 
     livingRoomTv.getState() 
     mainMenu()    

    else: 
     print("Welcome") 
     kitchenTv.setState(channel, show) 
     livingRoomTv.setState(channel, show) 
     saveFile() 
     mainMenu()  

startUp() 
+1

問題を解決する方法は、問題の_core_のみが残されるまで減らしてください。この場合;あなたが仕事に就くことができないものだけが残されるまであなたのプログラムからものを取り除く。ファイルから状態を読み込んで印刷するプログラムでしょうか?次に、その問題を解決し、他のすべてのものを戻します。 –

+0

[mcve]を投稿してください。私は、あなたがピクルスの問題を示すためにその壁を必要としているとは思わない。 – Goyo

+0

"_私は基本的にnet_全体を検索しました。"あなたは今かなり長い間このことをしてきたに違いありません。 –

答えて

1

でいただきありがとうございますので、save機能が動作することを説明し、問題ありませんが、グローバルへの割り当ては自動ではありません:あなたは、変数をグローバルにするために持っているか、それになります範囲外になるとすぐに失われるローカル変数を作成します(したがって、状態がリロードされない理由を説明します)。

グローバル変数はv関数にグローバルとして見られるように可変であるglobal vを添加することによって到達することができる。

def importFile():  
    global livingRoomTv # link to the global variable 
    global kitchenTv # link to the global variable 

    with open("tvState.pkl",'rb') as infile: 

     livingRoomTv = pickle.load(infile) 
     kitchenTv = pickle.load(infile) 

単純MCVE:

z=[1,2,3] 

def load(): 
    #global z 
    z=set() 

load() 

print(z) 

プリント[1,2,3]

global zのコメントを外して、set([])を印刷します。これは私が成功したことを意味します関数内でzを変更することができます。

+0

すみません、ありがとうございました。あなたは私の問題を解決しました..私は本当にあなたがコードの全体の "壁"を通過したことに感謝します、それは乱雑ですので、次回は私はコードを最小化しようとします。 –

+1

私は "壁"を通らなかった。私はちょうど両方のロード/セーブルーチンをチェックして、それがグローバルについてであることを理解しました(実際のトラップなので、私はしばしばシングルトンクラスを使用するので、メンバ変数だけを処理でき、グローバルは処理できません)。私の[mcve]は5行であることに注意してください。 –

関連する問題