【发布时间】:2020-03-28 21:26:26
【问题描述】:
我在按顺序单独打印列表时遇到问题。你能告诉我我做错了什么以及如何解决这个问题吗?它只会打印出列表,而不是我想要打印出的实际值。
类定义
class Radio:
radioStation = 0
radioMap = ["STATIC", "97.2", "99.6", "101.7", "105.3", "108.5"]
def __init__(self):
self.preset1 = Radio.radioMap[0]
self.preset2 = Radio.radioMap[0]
self.preset3 = Radio.radioMap[0]
## save "STATIC" plus the 5 stations to a list, starting at the zero position
## set each of the 3 presets to STATIC (0 location in the list), as well as
## the currently tuned in variable
## call the displayLCD method
def seekNext(self):
Radio.radioStation += 1
for i in range(5):
print(Radio.radioMap)
self.displayLCD()
## add 1 to the currently tuned in variable
## if the currently turned in variable is greater then 5, set the currently tuned in variable to 1
## call the displayLCD melthod
def longPressPreset1(self):
self.preset1 = Radio.radioMap
self.displayLCD()
## set preset1 variable to currently turned in
## call the displayLCD melthod
def longPressPreset2(self):
self.preset2 = Radio.radioMap
self.displayLCD()
## set preset2 variable to currently turned in
## call the displayLCD melthod
def longPressPreset3(self):
self.preset3 = Radio.radioMap
self.displayLCD()
## set preset3 variable to the currently turned in
## call the displayLCD melthod
def displayLCD(self):
return print("\nCurrently tuned: ", Radio.radioMap)
## print the currently tuned in line using the currently turned in variable as the index into
## the station list
def __str__(self):
return "\nPreset 1: " + " " + str(self.preset1) + " " + "\nPreset 2: "+ " " + str(self.preset2) + " " + "\nPreset 3: " + " " + str(self.preset3)+ " " +"\nCurrently tuned: " + " " + str(Radio.radioMap)
## RETURN the three preset values and the currently turned in station in the
## required format AS A STRING
程序的主要功能
def main():
myRadio = Radio()
option = displayMenuGetOption()
while option != "10":
if option == "1":
myRadio.displayLCD()
elif option == "2":
myRadio.longPressPreset1()
elif option == "3":
myRadio.longPressPreset2()
elif option == "4":
myRadio.longPressPreset3()
elif option == "5":
myRadio.seekNext()
elif option == "6":
myRadio.shortPressPreset1()
elif option == "7":
myRadio.shortPressPreset2()
elif option == "8":
myRadio.shortPressPreset3()
elif option == "9":
print(myRadio)
option = displayMenuGetOption()
def displayMenuGetOption():
print("\n1 = Display tuned in station")
print("2 = Program preset station 1")
print("3 = Program preset station 2")
print("4 = Program preset station 3")
print("5 = Seek next station")
print("6 = Tune preset station 1")
print("7 = Tune preset station 2")
print("8 = Tune preset station 3")
print("9 = Dump Programming")
print("10 = Turn off radio")
return input("\nEnter option: ")
main()
input("\nRun complete. Press the Enter key to exit.")
这是打印出来的
['STATIC', '97.2', '99.6', '101.7', '105.3', '108.5']
Currently tuned: ['STATIC', '97.2', '99.6', '101.7', '105.3', '108.5']
['STATIC', '97.2', '99.6', '101.7', '105.3', '108.5']
Currently tuned: ['STATIC', '97.2', '99.6', '101.7', '105.3', '108.5']
['STATIC', '97.2', '99.6', '101.7', '105.3', '108.5']
Currently tuned: ['STATIC', '97.2', '99.6', '101.7', '105.3', '108.5']
['STATIC', '97.2', '99.6', '101.7', '105.3', '108.5']
Currently tuned: ['STATIC', '97.2', '99.6', '101.7', '105.3', '108.5']
['STATIC', '97.2', '99.6', '101.7', '105.3', '108.5']
Currently tuned: ['STATIC', '97.2', '99.6', '101.7', '105.3', '108.5']
这就是我想要的样子
enter option: 1
Currently tuned: STATIC
enter option: 5
Currently tuned: STATIC
enter option: 5
Currently Tuned: 97.2
enter option: 2
Currently Tuned: 97.2
enter option: 5
Currently tuned: 99.6
enter option: 4
Currently tuned: 99.6
enter option: 6
Preset 1: 97.2
Preset 2: STATIC
Preset 3: 99.6
Currently tuned: 99.6
【问题讨论】: