Python 我想实现一个循环,但我不';我不知道该怎么做
好吧,那么Python 我想实现一个循环,但我不';我不知道该怎么做,python,loops,python-3.x,Python,Loops,Python 3.x,好吧,那么 print("What do you wanna do?") input1 = input() if input1 == "Stab this guy" or input1 == "stab this guy": print("You stabbed that guy and killed him.") elif input1 == "Punch this guy" or input1 == "punch this guy": print("You punc
print("What do you wanna do?")
input1 = input()
if input1 == "Stab this guy" or input1 == "stab this guy":
print("You stabbed that guy and killed him.")
elif input1 == "Punch this guy" or input1 == "punch this guy":
print("You punched him...")
我想尝试用这些复杂的输入创建一个循环,因此,当您输入一个仍然复杂但未提及的输入时,我希望它打印类似“对不起,没有捕捉到”的内容,这样他们就必须重复输入列表中包含的不同输入,直到他们说出正确的输入为止。要在您已有的基础上继续:
print("What do you wanna do?")
input1 = input()
complex_inputs = ["stab this guy", "punch this guy"]
while input1.lower() not in complex_inputs:
print("Sorry didn't catch that")
print("What do you wanna do?")
input1 = input()
if input1 == "Stab this guy" or input1 == "stab this guy":
print("You stabbed that guy and killed him.")
elif input1 == "Punch this guy" or input1 == "punch this guy":
print("You punched him...")
编辑:
要同时处理关键字,这是一个想法:
print("What do you wanna do?")
input1 = input()
complex_inputs = ["stab this guy", "punch this guy"]
actions_list = [action for actions in complex_inputs for action in actions.split()]
while input1.lower() not in (actions_list or complex_inputs):
print("Sorry didn't catch that")
print("What do you wanna do?")
input1 = input()
if input1.lower() in "stab this guy":
print("You stabbed that guy and killed him.")
elif (input1.lower() in "punch this guy"):
print("You punched him...")
您正在查看一个
while
循环,例如while True:
将永远循环,除非您明确地将从循环中断开。然而,在while循环中,您不能有复杂的输入,或者这是不真实的,因为我尝试过这种方法,但无法找到处理复杂输入的方法。这不起作用。如果你有一个大写字母,例如“Punch this guy”,与“Punch this guy”相比,只有“Punch this guy”可以工作,因此不能改变你的输入,这正是我想要的,我的用户可以在他们的输入中体验自由。你应该用所有小写字符串定义复杂的输入。您的检查将是,而input1.lower()不在复杂的输入中:
很好,感谢您捕捉并指出,对于更复杂的情况,是否有可能添加两个以上的输入,因此对于Punch,这个家伙是否可以添加“Punch”?当然,只需在复杂输入列表中添加更多输入,如下所示:complex\u input1.lower()=“punch”:print(“a punch…)
,如果您需要相应的响应,请在下面添加它,就像您对其他输入所做的那样:elif input1.lower()=“punch”:print(“a punch…)