Python 3 if/elif 个问题

Python 3 if/elif issue

我在下面的代码片段中有一个 if/elif 语句...出于某种原因,即使我触发了 elif 语句,控制台仍然打印 if 语句而不是 elif...我确定我错过了一些愚蠢的东西。

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("")

这是因为您在 "You bet" 之后的第一个 if 语句中缺少 in use_stim

  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("")

现在应该可以了。

如果需要,给您一些注意事项;

  • 您可以使用 /n 在打印语句之间添加额外的行,这样您就可以删除 print("") 语句并将 \n 添加到前面的语句中,例如;

    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
    
  • 使用 in 将搜索您在字符串中 任意位置 指定的文本,因此 "no" in use_stim 将起作用对于 nonopeno waynot a chance,因此您可以将 elif 语句简化为;

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