简体   繁体   中英

If, if-else, and elif statements

I know how to do an if statement and elif statement but in this particular code i don't know how. What im trying to do exactly is when the user puts either female or male and a temperature it will show an outfit for that weather depending which gender is chosen. Can anyone help me?

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

Assuming the gender typed has to be either male or female , this should work.

On a sidenote this code is not very pretty and I recommend looking up f-strings to parse 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()

I have edited your code to add nested if-statements and made it a little more user-friendly. I added the items like you asked and also guided you on how you would write other items:

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

The casefold() method makes sure that capitals do not matter so the user can answer the gender question with varying capitals and still get to the output.

Changes made in the conditions as well as added a gender check:

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

OUTPUT:

================================================                                                                                                                             
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

You have to nest multiple if-statements:

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

use and . in your case, it can be

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

Here is how you can add an if statement to
make the outputted outfit name be customized for males and females :

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

Input:

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

Output:

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

(note: You've goth the strict inequality operators mixed up, which I fixed here.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM