if,if-else和elif语句

时间:2020-06-23 20:14:47

标签: python if-statement

我知道如何执行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()

6 个答案:

答案 0 :(得分:1)

假设键入的性别必须为malefemale,这应该可行。

在旁注中,这段代码不是很漂亮,我建议您查找f字符串以解析字符串。

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

答案 1 :(得分: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 :(得分:0)

对条件进行了更改并添加了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

答案 3 :(得分:0)

您必须嵌套多个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")

答案 4 :(得分:0)

使用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")

答案 5 :(得分:0)

您可以在此处添加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

(注意:您混合了严格的不等式运算符,已在此处将其修复。)