I need some help starting of this radio program

Multi tool use
Multi tool use


I need some help starting of this radio program



What I need help on is getting the stations to change to the next station in the list if the user presses 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



It starts at static but I want it to change every single one and then start over again. I tried something like this:


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



I only end up getting the last station cannot figure out how to list the rest of the stations.





Using negative indices with lists operates on them in reverse order, so -1 is the last item, -2, second last and so on.
– marienbad
Apr 17 '16 at 5:23




3 Answers
3



There are a couple of reasonable ways to do what you want.



The easiest would be to make your class store an index into your list, rather than an list item directly. That way you can increment the index and wrap it around using the % modulus operator:


%


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", and possibly more Pythonic way to solve the problem would be to use the cycle generator from the itertools module in the Python standard library. It returns an iterator that yields the values from your list, starting over when it reaches the end. Though you usually only deal with iterators in for loops, it's easy to use the iterator protocol by hand too. In our case, we just want to call next on the iterator to get the next value:


cycle


itertools


for


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)


index = 0

if a=="3":
index = (index+1)%6
print(stations[index])





Using len(stations) instead of just 6 would be more general.
– zegkljan
Apr 17 '16 at 5:35


len(stations)


6



Define a variable in init with the actual position "self.pos = 0", and call this function when you required


def seekNext(self):
if(self.pos == (len(self.stations)-1)):
self.pos = 0
else:
self.pos += 1
print(self.stations[self.pos])





This increases the counter only in the case it reached the end.
– zegkljan
Apr 17 '16 at 5:44






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

23JNyvC4S,0YH,0bScNA0nwOgaQlrjsqqK,WsqkUEDDdjN4,o5,xXW SqOTQdK6QJe
WQ3KpAiIBCwzGnX0GHlrW,4yW4GTtXoF8 YJ,WfkdxnaHiEdW qkr2RUkvfWEKU

Popular posts from this blog

Rothschild family

Cinema of Italy