gpt4 book ai didi

python - if、if-else 和 elif 语句

转载 作者:行者123 更新时间:2023-12-04 09:36:43 25 4
gpt4 key购买 nike

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

最佳答案

我已经编辑了您的代码以添加嵌套的 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()方法确保大写无关紧要,因此用户可以用不同的大写回答性别问题,并且仍然可以得到输出。

关于python - if、if-else 和 elif 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62543031/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com