制作电视模拟器-Python
我对Python真的很陌生,我必须制作一个电视模拟器。我基本上搜索了整个网络,但我真的找不到我的答案。我的问题是,我们必须将电视的状态保存在一个文件中,当我们重新打开节目时,它必须检索其以前的状态,即频道、节目、音量。我使用pickle保存文件,但它不检索以前的状态 希望我已经非常具体,否则请随时要求我澄清 先谢谢你制作电视模拟器-Python,python,pickle,Python,Pickle,我对Python真的很陌生,我必须制作一个电视模拟器。我基本上搜索了整个网络,但我真的找不到我的答案。我的问题是,我们必须将电视的状态保存在一个文件中,当我们重新打开节目时,它必须检索其以前的状态,即频道、节目、音量。我使用pickle保存文件,但它不检索以前的状态 希望我已经非常具体,否则请随时要求我澄清 先谢谢你 class Television(object): currentState = [] def __init__(self,volume,channel,show
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()
使用全局变量进行读取没有问题,因此解释了
保存
功能可以工作,但全局变量的赋值不是自动的:您必须将变量设置为全局变量,否则它将创建局部变量,这些变量一旦超出范围就会丢失(从而解释了为什么您的状态没有重新加载)
通过添加global v
可以获得全局变量,其中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
解决任何问题的方法就是减少它,直到你只剩下问题的核心。在这种情况下;删除程序中的内容,直到只剩下无法工作的内容。也许这是一个从文件中读取状态并打印的程序?然后你解决了这个问题,把其他的东西都放回去。请发一个帖子。我认为你不需要那堵代码墙来显示你对pickle的问题。“我基本上搜索了整个网络”你一定已经这样做了很长时间了。@MagnusHoff谢谢你的建议。下次我会试试。@Goyo问题现在解决了,但我会保留它以备将来参考。这只是因为我是一个新的编码,所以我不知道到底是什么会影响程序。非常感谢你的快速回答。你解决了我的问题。。我真的很感激你看穿了整个代码的“墙”,我知道它很混乱,所以下次我有问题时,我会尽量减少代码。我没有看穿“墙”。我只是检查了两个加载/保存例程,了解到这是关于全局变量的(这是一个真正的陷阱,所以我经常使用单例类,这样我就可以只操作成员变量,而不操作全局变量)。注意我的,它有5行长。