0
import time
while True:
    npc=input("Josuke\n\nJotaro Kujo (Part 4)\n\nChoose NPC to talk to.")
    if npc=="Josuke" or npc=="josuke":
        confirm=input("[Press E to interact.]")
    elif npc=="jp4" or npc=="JP4" or npc=="Jp4

Within this code you can see that there are 2 NPCs to interact to. Because there are many ways of addressing the name Jotaro Kujo (Part 4), the if statement has many "or"s, but I want to be able to condense it. Could I use an array to be able to put the possibilities in and then have the if statement identify if the value is within the array? (I haven't completed the code yet, but the problem doesn't require the full code to be completed.)

  • 1
    Are you only looking for case variations? ``npc.casefold() == "jp4"`` would cover all three cases of the ``elif``, for example. – MisterMiyagi Jan 11 '22 at 13:32
  • 1
    Does this answer your question? [How do I do a case-insensitive string comparison?](https://stackoverflow.com/questions/319426/how-do-i-do-a-case-insensitive-string-comparison) – MisterMiyagi Jan 11 '22 at 13:35

1 Answers1

1

Yes, you can do it easily using the in operator to check if a particular string is in the list.

as an example:

lst = ["bemwa", "mike", "charles"]

if "bemwa" in lst:
    print("found")

And if all the possibilities you want to cover are related to case-insensitivity you can simply convert the input to lower-case or upper-case and compare it with only one possibility.

Bemwa Malak
  • 1,182
  • 1
  • 5
  • 18