0

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("")
Stagger
  • 115
  • 2
  • 3
  • 11

1 Answers1

1

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:

Nordle
  • 2,915
  • 3
  • 16
  • 34
  • oh man... i cant believe I missed that... rookie mistake... as a beginner programmer your notes help me a lot and I will definitely implement them into my code...thanks a lot – Stagger Dec 11 '18 at 11:37
  • Happens to the best of us! And not a problem at all, glad to help :) – Nordle Dec 11 '18 at 11:49
  • Matt, I tried the /n method like in your example but it's not working. It just prints /n as part of the string instead of making a new line. I am using qPython on my phone. Might be an issue with the IDE? – Stagger Dec 11 '18 at 12:36
  • Sorry @Stagger I'm not sure why it wouldn't interpret it as a newline and I'm not too familiar with qPython, there could well be a way to change the settings to interpret the `/n` as a newline within qPython itself. If not then revert back to printing nothing, it's not exactly bad code it just cleans it up slightly! – Nordle Dec 11 '18 at 13:29
  • 1
    i found the problem...it's \n not /n – Stagger Dec 12 '18 at 10:11
  • ...sorry @Stagger, indeed it is.. I shouldn't answer questions before having a coffee.. – Nordle Dec 12 '18 at 10:16
  • 1
    You still helped me a lot and solved my problem... imagine what you can do after coffee. – Stagger Dec 12 '18 at 10:19