如果为true,则从现在开始在Python中忽略变量
我有一个任务,它迫使我忽略某些变量,如果条件已满。基本上,我要求用户输入并告诉他他有哪些有效的选择,但过了一段时间,原来有效的选择不再有效。我想过做这样的事如果为true,则从现在开始在Python中忽略变量,python,variables,if-statement,input,ignore,Python,Variables,If Statement,Input,Ignore,我有一个任务,它迫使我忽略某些变量,如果条件已满。基本上,我要求用户输入并告诉他他有哪些有效的选择,但过了一段时间,原来有效的选择不再有效。我想过做这样的事 while True: choice = input('You can choose between: ', Choice1, Choice2, Choice3) if choice == Choice1: Choice1Counter +=1 break elif choice ==
while True:
choice = input('You can choose between: ', Choice1, Choice2, Choice3)
if choice == Choice1:
Choice1Counter +=1
break
elif choice == Choice2:
Choice2Counter +=1
break
elif choice == Choice3:
Choice2Counter +=1
break
else:
choice = input('You can choose between: ', Choice1, Choice2, Choice3)
continue
if Choice1Chounter == 4:
#ignore Choice 1 for the rest of the Programm or until Choice1 is reset
有了这个,我将首先“强制”一个有效的选项,如果输入是一个有效的选项,我将在该选项的计数器上加1。如果计数器达到它的极限,我想做这样的事情
while True:
choice = input('You can choose between: ', Choice1, Choice2, Choice3)
if choice == Choice1:
Choice1Counter +=1
break
elif choice == Choice2:
Choice2Counter +=1
break
elif choice == Choice3:
Choice2Counter +=1
break
else:
choice = input('You can choose between: ', Choice1, Choice2, Choice3)
continue
if Choice1Chounter == 4:
#ignore Choice 1 for the rest of the Programm or until Choice1 is reset
这基本上意味着程序会忽略Choice1,这看起来有点像这样(在我看来)
choice=Input('您可以选择:',Choice1,Choice2,Choice3)
因此,在Choice1Counter达到ist限制后运行程序时,它基本上应该“打印”出以下内容
您可以选择:Choice2 Choice3
我有82个有效输入,不能真正定义所有82个!它们的组合,所以我考虑过这一点,但找不到一个只忽略程序其余部分变量的命令。您不应该为此使用单独的变量,而应该使用字典和当前有效键的列表
choices = ["Choice1", "Choice2", "Choice3", "Choice4"]
counters = dict((choice, 0) for choice in choices)
while choices: # exit when no choices left
choice = raw_input("Choose from %s > " % " ".join(choices)) # input in Py3
if choice in choices:
counters[choice] += 1
if counters[choice] == 4:
choices.remove(choice)
else:
print("That choice is not valid. Try again")
您不应该为此使用单独的变量,而应该使用字典和当前有效键的列表
choices = ["Choice1", "Choice2", "Choice3", "Choice4"]
counters = dict((choice, 0) for choice in choices)
while choices: # exit when no choices left
choice = raw_input("Choose from %s > " % " ".join(choices)) # input in Py3
if choice in choices:
counters[choice] += 1
if counters[choice] == 4:
choices.remove(choice)
else:
print("That choice is not valid. Try again")
使用一些变量
True/False
来控制忽略哪个选项,即,ignore=[False,False,True]
,然后您可以使用它来决定使用哪个元素。顺便说一句:您可以使用列表choice[0]
,choice[1],等等和choice\u计数器[0]
,choice\u计数器[1]
,然后你可以使用for
循环来处理这些元素。使用一些变量True/False
来控制忽略哪个选项,即,ignore=[False,False,True]
,然后你可以用它来决定使用哪个元素。顺便说一句:你可以使用列表choice[0]
,choice[1],etc
和选择计数器[0]
,选择计数器[1]
等。然后您可以使用for
循环来处理这些元素。非常感谢您的帮助。我刚试过,效果很好。非常感谢你的帮助。我刚试过,效果很好。