Search code examples
qpython3

Python 3 if/elif issue


i have a if/elif statement in my code snippet below... for some reason even when i trigger the elif statement the console still prints the if statement instead of the elif... i'm sure i am missing something stupid.

use_stim = str.lower(input("Do you want to use this stimpak now?"))

    if "ok" in use_stim or "okay" in use_stim or "yes" in use_stim or "sure" in use_stim or "you bet" or "good idea" in use_stim:
        char_health = char_health + char_stim_heal
        char_stims = char_stims - 1
        print(char_name + ": Using stimpak, I feel better")  
        print("")
        print("***STATS***")
        print("Health:" + str(char_health)) 
        print("Stimpaks:" + str(char_stims))
        print("***STATS***")
        print("")            
        search_next = str.lower(input(char_name + ": Alright, what's next? Weapons or amunition?"))
        print(username + ": " + search_next)

    elif "no" in use_stim or "nope" in use_stim or "never" in use_stim or "no way" in use_stim or "not a chance" in use_stim or "i don't think so" in use_stim:
        print(user_name + ": " + use_stim)
        print(char_name + ": Alright, i'll save it for later.")
        char_stims = 1
        char_health = 75
        print("")
        print("***STATS***")
        print("Health:" + str(char_health)) 
        print("Stimpaks:" + str(char_stims))
        print("***STATS***")
        print("")

Solution

  • It's because you're missing an in use_stim in the first if statement after the "You bet";

      if "ok" in use_stim or "okay" in use_stim or "yes" in use_stim or "sure" in use_stim or "you bet" in use_stim or "good idea" in use_stim:
            char_health = char_health + char_stim_heal
            char_stims = char_stims - 1
            print(char_name + ": Using stimpak, I feel better")  
            print("")
            print("***STATS***")
            print("Health:" + str(char_health)) 
            print("Stimpaks:" + str(char_stims))
            print("***STATS***")
            print("")            
            search_next = str.lower(input(char_name + ": Alright, what's next? Weapons or amunition?"))
            print(username + ": " + search_next)
    
        elif "no" in use_stim or "nope" in use_stim or "never" in use_stim or "no way" in use_stim or "not a chance" in use_stim or "i don't think so" in use_stim:
            print(user_name + ": " + use_stim)
            print(char_name + ": Alright, i'll save it for later.")
            char_stims = 1
            char_health = 75
            print("")
            print("***STATS***")
            print("Health:" + str(char_health)) 
            print("Stimpaks:" + str(char_stims))
            print("***STATS***")
            print("")
    

    This should work now.

    A couple of notes for you if you want;

    • You can add the extra line between print statements using /n so you can remove the print("") statements and add \n to the previous statement e.g;

      print(char_name + ": Using stimpak, I feel better\n") <<< Add \n here  
      print("")                                             <<< Remove this
      print("***STATS***")
      print("Health:" + str(char_health)) 
      print("Stimpaks:" + str(char_stims))
      print("***STATS***\n")                                <<< Add \n here 
      print("")                                             <<< Remove this
      
    • The use of in will search for the text you've specified anywhere in the string, so "no" in use_stim will work for no, nope, no way and not a chance, therefore you can simplify the elif statement to;

    elif "no" in use_stim or "never" in use_stim or "i don't think so" in use_stim: