【问题标题】:If, if-else, and elif statementsIf、if-else 和 elif 语句
【发布时间】:2020-06-23 20:14:47
【问题描述】:

我知道如何执行 if 语句和 elif 语句,但在此特定代码中我不知道如何执行。我试图做的是当用户放置女性或男性和温度时,它将根据选择的性别显示适合该天气的服装。谁能帮帮我?

def main():
    
    print("Welcome to Outfitters Boulevard!")
    print()
    print("Where your next vacation outfit is just around the corner.")
    print("================================================")
    
    name = input("What is your name?")
    gender = input("Are you male or female?")
    favoriteColor = input("What is your favorite color?")
    temp = int(input("What is the temperature like where you're going?"))
    
    print(name + ", you mentioned that you're a female, your favorite color is " + favoriteColor + ",")
    print("and you're going somewhere with " + str(temp) + " weather.")
    print("We've got just the outfit for you: ")
    
    if temp>84:
        print("The Perfect Hot Weather Outfit")
    elif 70 >=temp<= 84:
        print("The Perfect Warm Weather Outfit")
    elif 55>=temp<= 69:
        print("The Perfect Cool Weather Outfit")
    elif temp <55: 
        print("The Perfect Cold Weather Outfit")
    
main()

【问题讨论】:

  • 会显示哪种服装适合哪种性别?
  • 对于“完美的炎热天气服装”,如果用户是女性,它将显示短裤、凉鞋、太阳帽和短款衬衫,但如果用户是男性,它将显示夏威夷衬衫、短裤和人字拖
  • 您的比较语法错误。应该是elif 70 &lt;= temp &lt;= 84:
  • 另请注意,在带有“您提到您是女性”的打印语句中,您应该使用从输入中获得的 gender 变量
  • @anonymous123 我已经使用您提供的示例回答了您的问题。

标签: python if-statement


【解决方案1】:

我已经编辑了您的代码以添加嵌套的 if 语句并使其更加用户友好。我添加了您要求的项目,并指导您如何编写其他项目:

def main():
    
    print("Welcome to Outfitters Boulevard!")
    print()
    print("Where your next vacation outfit is just around the corner.")
    print("================================================")
    
    name = input("What is your name?")
    gender = input("Are you male or female or other?")
    favoriteColor = input("What is your favorite color?")
    temp = int(input("What is the temperature like where you're going?"))
    
    print(name + ", you mentioned that you're a female, your favorite color is " + favoriteColor + ",")
    print("and you're going somewhere with " + str(temp) + " weather.")
    print("We've got just the outfit for you: ")
    
    if temp>84:
        print("The Perfect Hot Weather Outfit")
        if gender.casefold() == 'male':
            # Show male outfits here
            print("Hawaiian shirt, shorts and flip flops")

        elif gender.casefold() == 'female':
            # Show female outfits here
            print("Shorts, sandals and a crop shirt.")

        else:
            # Show other outfits here

    elif 70 >=temp<= 84:
        print("The Perfect Warm Weather Outfit")
        if gender.casefold() == 'male':
            # Show male outfits here

        elif gender.casefold() == 'female':
            # Show female outfits here

        else:
            # Show other outfits here

    elif 55>=temp<= 69:
        print("The Perfect Cool Weather Outfit")
        if gender.casefold() == 'male':
            # Show male outfits here

        elif gender.casefold() == 'female':
            # Show female outfits here

        else:
            # Show other outfits here

    elif temp <55: 
        print("The Perfect Cold Weather Outfit")
        if gender.casefold() == 'male':
            # Show male outfits here

        elif gender.casefold() == 'female':
            # Show female outfits here

        else:
            # Show other outfits here
    
main()

casefold() 方法确保大写无关紧要,因此用户可以用不同的大写回答性别问题,并且仍然可以得到输出。

【讨论】:

    【解决方案2】:

    假设输入的性别必须是 malefemale,这应该可以。

    顺便说一句,这段代码不是很漂亮,我建议查找 f-strings 来解析字符串。

    def main():
        
        print("Welcome to Outfitters Boulevard!")
        print()
        print("Where your next vacation outfit is just around the corner.")
        print("================================================")
        
        name = input("What is your name?")
        gender = input("Are you male or female?")
        favoriteColor = input("What is your favorite color?")
        temp = int(input("What is the temperature like where you're going?"))
        
        print(name + ", you mentioned that you're a female, your favorite color is " + favoriteColor + ",")
        print("and you're going somewhere with " + str(temp) + " weather.")
        print("We've got just the outfit for you: ")
    
        # If it is a male
        if gender == "male":
           if temp>84:
              print("The Perfect Hot Weather Outfit")
           elif 70 >=temp<= 84:
              print("The Perfect Warm Weather Outfit")
           elif 55>=temp<= 69:
              print("The Perfect Cool Weather Outfit")
           elif temp <55: 
              print("The Perfect Cold Weather Outfit")
        # if it is a female
        elif gender == "female":
           if temp>84:
              print("The Perfect Hot Weather Outfit")
           elif 70 >=temp<= 84:
              print("The Perfect Warm Weather Outfit")
           elif 55>=temp<= 69:
              print("The Perfect Cool Weather Outfit")
           elif temp <55: 
              print("The Perfect Cold Weather Outfit")
        # If the gender is not correct
        else:
           print(f"Gender has to be male or female (found {gender})")
    
    main()
    

    【讨论】:

      【解决方案3】:

      对条件进行了更改并添加了gender检查:

      print("Welcome to Outfitters Boulevard!")
      print()
      print("Where your next vacation outfit is just around the corner.")
      print("================================================")
      
      name = input("What is your name?")
      gender = input("Are you male or female?")
      favoriteColor = input("What is your favorite color?")
      temp = int(input("What is the temperature like where you're going?"))
      
      if gender.lower() == "yes":
          gender = "female"
      else:
          gender = "male"
      print(name + ", you mentioned that you're a " + gender + ", your favorite color is " + favoriteColor + ",")
      print("and you're going somewhere with " + str(temp) + " weather.")
      print("We've got just the outfit for you: ")
      
      if temp > 84:
          print("The Perfect Hot Weather Outfit")
      elif temp >= 70 and temp <= 84:
          print("The Perfect Warm Weather Outfit")
      elif temp >= 55 and temp <= 69:
          print("The Perfect Cool Weather Outfit")
      elif temp < 55: 
          print("The Perfect Cold Weather Outfit")
      

      输出:

      ================================================                                                                                                                             
      What is your name?dirtybit                                                                                                                                                   
      Are you male or female?no                                                                                                                                                    
      What is your favorite color?black                                                                                                                                            
      What is the temperature like where you're going?75                                                                                                                           
      dirtybit, you mentioned that you're a male, your favorite color is black,                                                                                                     
      and you're going somewhere with 75 weather.                                                                                                                                  
      We've got just the outfit for you:                                                                                                                                           
      The Perfect Warm Weather Outfit
      

      【讨论】:

      • 用户如何知道输入“是”或“否”来回答这个问题:Are you male or female?我看不出这部分有什么意义?
      • 我确定您将Are you male or female? 误读为Are you female?
      【解决方案4】:

      你必须嵌套多个 if 语句:

      if gender == "female":
          if temp>84:
              print("The Perfect Hot Weather Outfit")
          elif 70 >=temp<= 84:
              print("The Perfect Warm Weather Outfit")
          elif 55>=temp<= 69:
              print("The Perfect Cool Weather Outfit")
          elif temp <55: 
              print("The Perfect Cold Weather Outfit")
      else:
          if temp>84:
              print("The Perfect Hot Weather Outfit")
          elif 70 >=temp<= 84:
              print("The Perfect Warm Weather Outfit")
          elif 55>=temp<= 69:
              print("The Perfect Cool Weather Outfit")
          elif temp <55: 
              print("The Perfect Cold Weather Outfit")
      

      【讨论】:

        【解决方案5】:

        使用and。在你的情况下,它可以是

        elif temp >= 70 and temp <= 84:
            print("The Perfect Warm Weather Outfit")
        elif temp 55 >= and temp <= 69:
            print("The Perfect Cool Weather Outfit")
        elif temp < 55: 
            print("The Perfect Cold Weather Outfit")
        
        

        【讨论】:

          【解决方案6】:

          您可以通过以下方式添加if 语句以
          使输出的服装名称为男性和女性定制

          def main():
              
              print("Welcome to Outfitters Boulevard!\n")
              print("Where your next vacation outfit is just around the corner.")
              print("================================================")
              
              name = input("What is your name?")
              gender = input("Are you male or female?")
              favoriteColor = input("What is your favorite color?")
              temp = int(input("What is the temperature like where you're going?"))
              
              print(f"{name}, you mentioned that you're a female, your favorite color is {favoriteColor},")
              print(f"and you're going somewhere with {temp} weather.")
              print("We've got just the outfit for you: ")
              
              if gender == 'female': # Here is where the customization begins
                  gender = 'For Women'
              else:
                  gender = 'For Men'
              if temp > 84:
                  print(f"The Perfect Hot Weather Outfit {gender}")
              elif 70 <= temp <= 84:
                  print(f"The Perfect Warm Weather Outfit {gender}")
              elif 55 <= temp <= 69:
                  print(f"The Perfect Cool Weather Outfit {gender}")
              elif temp < 55: 
                  print(f"The Perfect Cold Weather Outfit {gender}")
              
          main()
          

          输入:

          What is your name? Ann Zen
          Are you male or female? female
          What is your favorite color? red
          What is the temperature like where you're going? 70
          

          输出:

          Ann Zen, you mentioned that you're a female, your favorite color is red,
          and you're going somewhere with 70 weather.
          We've got just the outfit for you: 
          The Perfect Warm Weather Outfit For Women
          

          (注意:你把严格的不等式运算符弄混了,我在这里修复了。)

          【讨论】: