我需要帮助制作一个用作遥控器的程序。我不确定如何从用户可以选择的不同选项中制作列表。然后,如果不正确,请继续询问,直到用户关闭电视电源。它不断给我缩进错误,我需要一些关于我做错了什么的建议,也许是一些格式化和帮助简化我的代码,但自从我在 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
. -
最后 2 行的缩进是错误的:
print()
调用需要在while
循环内。 -
将
print()
函数的值分配给op1
等除了打印指令之外没有任何用处。变量都将具有值None
。
while(True):
X= input("enter the command")
"""do what ever you want here""""
这将始终要求单个变量,您可以检查if/else。 如果要保留询问语句,请使用 hold。