我需要一些帮助来开始这个广播节目
I need some help starting of this radio program
我需要帮助的是让电台在用户按 3 时更改为列表中的下一个电台。
class Radio:
def __init__(self):
self.stations=["STATIC","97.2", "99.6", "101.7", "105.3", "108.5"]
self.stationStart=self.stations[0]
def seekNext(self):
self.stationsStart
它从静态开始,但我希望它改变每一个然后重新开始。我试过这样的事情:
stations=["STATIC","97.2", "99.6", "101.7", "105.3", "108.5"]
a =input("enter 3 to seek next")
while a !="0":
if a =="3":
print(stations[-1])
我只得到最后一个电台不知道如何列出其余的电台。
index = 0
if a=="3":
index = (index+1)%6
print(stations[index])
有几种合理的方法可以满足您的需求。
最简单的方法是让 class 将索引存储到列表中,而不是直接存储列表项。这样你就可以增加索引并使用 %
模数运算符将其环绕:
class Radio:
def __init__(self):
self.stations=["STATIC","97.2", "99.6", "101.7", "105.3", "108.5"]
self.station_index = 0
def seek(self):
print("Currently tuned to", self.stations[self.station_index])
print("Seeking...")
self.station_index = (self.station_index + 1) % len(self.stations)
print("Now tuned to", self.stations[self.station_index])
A "fancier",可能还有更多 Pythonic 解决问题的方法是使用 [=25] 中 itertools
模块的 cycle
生成器=] 标准库。它 returns 一个迭代器,它从你的列表中产生值,当它到达末尾时重新开始。虽然您通常只在 for
循环中处理迭代器,但手动使用 iterator protocol 也很容易。在我们的例子中,我们只想在迭代器上调用 next
来获取下一个值:
import itertools
class Radio:
def __init__(self):
self.stations = itertools.cycle(["STATIC","97.2", "99.6", "101.7", "105.3", "108.5"])
self.current_station = next(self.stations)
def seek(self):
print("Currently tuned to", self.current_station)
print("Seeking...")
self.current_station = next(self.stations)
print("Now tuned to", self.current_station)
在init中用实际位置定义一个变量"self.pos = 0",需要时调用此函数
def seekNext(self):
if(self.pos == (len(self.stations)-1)):
self.pos = 0
else:
self.pos += 1
print(self.stations[self.pos])
我需要帮助的是让电台在用户按 3 时更改为列表中的下一个电台。
class Radio:
def __init__(self):
self.stations=["STATIC","97.2", "99.6", "101.7", "105.3", "108.5"]
self.stationStart=self.stations[0]
def seekNext(self):
self.stationsStart
它从静态开始,但我希望它改变每一个然后重新开始。我试过这样的事情:
stations=["STATIC","97.2", "99.6", "101.7", "105.3", "108.5"]
a =input("enter 3 to seek next")
while a !="0":
if a =="3":
print(stations[-1])
我只得到最后一个电台不知道如何列出其余的电台。
index = 0
if a=="3":
index = (index+1)%6
print(stations[index])
有几种合理的方法可以满足您的需求。
最简单的方法是让 class 将索引存储到列表中,而不是直接存储列表项。这样你就可以增加索引并使用 %
模数运算符将其环绕:
class Radio:
def __init__(self):
self.stations=["STATIC","97.2", "99.6", "101.7", "105.3", "108.5"]
self.station_index = 0
def seek(self):
print("Currently tuned to", self.stations[self.station_index])
print("Seeking...")
self.station_index = (self.station_index + 1) % len(self.stations)
print("Now tuned to", self.stations[self.station_index])
A "fancier",可能还有更多 Pythonic 解决问题的方法是使用 [=25] 中 itertools
模块的 cycle
生成器=] 标准库。它 returns 一个迭代器,它从你的列表中产生值,当它到达末尾时重新开始。虽然您通常只在 for
循环中处理迭代器,但手动使用 iterator protocol 也很容易。在我们的例子中,我们只想在迭代器上调用 next
来获取下一个值:
import itertools
class Radio:
def __init__(self):
self.stations = itertools.cycle(["STATIC","97.2", "99.6", "101.7", "105.3", "108.5"])
self.current_station = next(self.stations)
def seek(self):
print("Currently tuned to", self.current_station)
print("Seeking...")
self.current_station = next(self.stations)
print("Now tuned to", self.current_station)
在init中用实际位置定义一个变量"self.pos = 0",需要时调用此函数
def seekNext(self):
if(self.pos == (len(self.stations)-1)):
self.pos = 0
else:
self.pos += 1
print(self.stations[self.pos])