basically I already found the solution to my problem, but no matter how hard I try i just don't understand how it actually works. I'm writing a text based adventure game and I want to use if statements to check if the user entered any of my specified words. I don't want the user have to guess the exact word, as long as the specified word is found in the input string, it should return True. So after I did a lot of research, I found this solution somebody posted and it works perfectly:
choice = input("> ")
if any(word in choice for word in ("kill", "stab", "slay")):
print("You defeat the monster.")
else:
("The monster defeats you.")
No matter how hard I whack my brain, I still don't understand how any(word in choice for word in ("kill", "stab", "slay")): accomplishes this.I get what any() does and I know what for loops are, but I've never seen for used without an executable body and i can't find any resources online on why this works. Any help would be very much appreciated, i don't want to just copy and paste code without actually understanding it.
Aucun commentaire:
Enregistrer un commentaire