[英]How to make a remote control in Python 3 that keeps asking for an input from a list of commands?
我需要制作一个可用作遥控器的程序的帮助。 我不确定如何从用户可以选择的不同选项中列出。 然后,如果不正确,请继续询问直到用户关闭电视电源。 而且它一直给我缩进错误,我需要针对我做错的事情提供一些建议,也许需要一些格式设置并帮助简化我的代码,但是自从我在CS的第一学期开始以来就没有什么太先进的了。 它基本上是def / return,while / for循环以及if / elif / else语句。 以下是我的代码和说明(我不太了解)
码
op1 =print ("'P' or 'p' Turns the power on")
op2 =print ("‘1’,’2’,’3’,’4’,’5’ Set a channel to the specified number")
op3 =print ("‘C+’ or ‘c+’ Add one to the current channel number")
op4 =print ("‘C-’ or ‘c-’ Subtract one to the current channel number")
op5 =print ("‘V+’ or ‘v+’ Add one to the current volume level")
op6 =print ("‘V-’ or ‘v-’ Subtract one from the current volume level")
op7 =print ("‘M’ or ‘m’ Mute the current volume level")
op0 =print ("‘X’ or ‘x’ Turn off the tv and exit the program")
print()
powerOff=False
channel =3
volume = 5
while powerOff !=False:
choice = input("Choose one of the above options: ")
while choice is not "x" or "X":
if choice == "x" or "X":
powerOff == True
elif choice == "P" or "p":
powerOff=False
if choice == volume and powerOff == False:
if choice == "M" or "m":
volume = 0
elif choice == "V+" or "v+":
volume+= 1
elif choice == "V-" or "v-":
volume= volume - 1
elif choice == channel and powerOff == False:
if choice == "1" or "2" or "3" or "4" or "5":
channel == choice
elif choice == "C+" or "c+":
channel += 1
elif choice == "C-" or "c-":
channel = channel - 1
else
choice == powerOff
print (choice)
choice = choice
同样,我只需要建议即可学习如何以更简单的方式进行这项工作,以便将来我可以应用,谢谢
这里有两个错误。
您的外部while
循环将永远不会执行,因为您首先将powerOff
设置为False
。
if choice == "x" or "X":
则测试不执行您认为的操作。 需要为if choice.upper() == "X":
或者if choice in ("x","X"):
。 需要固定在9个位置,包括内部while
。
缩进在最后两行中是错误的: print()
调用需要在while
循环内。
将print()
函数的值分配给op1
等,除了打印指令外,没有任何用处。 所有变量的值都为None
。
while(True):
X= input("enter the command")
"""do what ever you want here""""
这总是要求一个变量,您可以检查是否/否则。 如果要保留询问语句,请使用hold。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.