[英]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.我知道如何执行 if 语句和 elif 语句,但在此特定代码中我不知道如何。 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.假设输入的性别必须是male
或female
,这应该有效。
On a sidenote this code is not very pretty and I recommend looking up f-strings to parse strings.顺便说一句,这段代码不是很漂亮,我建议查找 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()
I have edited your code to add nested if-statements and made it a little more user-friendly.我已经编辑了您的代码以添加嵌套的 if 语句并使其更加用户友好。 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. casefold()
方法确保大写无关紧要,因此用户可以使用不同的大写来回答性别问题,并且仍然可以访问 output。
Changes made in the conditions as well as added a gender
check:对条件进行了更改并添加了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")
OUTPUT: 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 语句:
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
.使用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以下是如何将if
语句添加到
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: 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.) (注意:你把严格的不等式运算符弄混了,我在这里修复了。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.