nested conditionals assignment in #python3

时间:2018-02-05 12:52:54

标签: python-3.x

I've been struggling with this code for almost a week now, and I've written it in so many different ways but I haven't found a single way that works 100 %. Anyway, I'll post the task and my latest attempt, can you tell me what i'm doing wrong ?:

task:

Program: shirt order First get input for color and size White has sizes L, M Blue has sizes M, S print available or unavailable, then print the order confirmation of color and size hint: set a variable "available = False" before nested if statements and

change to True if color and size are available

my code:

print ("welcome to our store, we have some great shirts on sale")

shirt_color = ("w" + "b")

shirt_size =("s" + "m" + "l")


available = False


first_color = input("please select a color: ")

choosen_color = first_color



if choosen_color.lower() in shirt_color.lower() == "w":

    second_size = input("please choose a size")


    choosen_size = second_size


    if choosen_size.lower() in shirt_size.lower() == "l":


        available = True


        print ("order confirmed")


        print (available)


    elif choosen_size.lower() in shirt_size.lower() == "m":

            available = True

    print ("order confirmed")

    print (available)

    if choosen_color.lower() in shirt_color.lower() == "b":

        second_size = input("please choose a size")

    choosen_size = second_size

    if choosen_size.lower() in shirt_size.lower() == "m":

        available = True

        print ("order confirmed")

        print (available)

    elif choosen_size.lower() in shirt_size.lower() == "s":

            available = True

    print ("order confirmed")

    print (available)

else:

    print ("not confirmed")

    print (available)

1 个答案:

答案 0 :(得分:0)

有问题的部分是以下列方式编码的条件语句: choosen_color.lower() in shirt_color.lower() == "w"

首先检查chosen_color是否有效(在允许的颜色中)是足够的:

choosen_color.lower() in shirt_color.lower()

并且如果它实际上是您要检查的颜色之一,则单独进行:

choosen_color.lower() in shirt_color.lower() and choosen_color.lower() == "w"

以相似的方式更改代码大小应该可以帮助您按照自己的意愿使代码工作。