Python 3 if / elif问题

时间:2018-12-11 10:25:56

标签: qpython3

我在下面的代码段中有一个if / elif语句...出于某种原因,即使当我触发elif语句时,控制台仍会打印if语句而不是elif ...我确定我丢失了某些内容愚蠢的。

  

use_stim = str.lower(input(“您现在要使用此stimpak吗?”))

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

1 个答案:

答案 0 :(得分:1)

这是因为您在in use_stim之后的第一个if语句中缺少了"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("")

现在应该可以使用。

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

  • 您可以使用/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: