衬衫订购程序

问题描述 投票:0回答:2

这是我在Microsoft的Python课程中完成的一项小任务的代码。我浏览了他们的论坛,但讨论页面都没有太大帮助。

“任务”

我感到困惑的是为什么我的if结束语句不运行,并且我不确定布尔如何工作,为什么我必须将available设置为False才能工作,然后再设置其余部分为真。为什么我不能只设置为True?

而且,我该如何以更简洁的方式编写代码?

#variable inputs
colour = input("What colour shirt? " ).lower()
size = input("What size? " ).lower()

#why do i set available to False?
available = False


if colour == "white":
    available == True
    if size == "m" or size == "l":
        available == True
        print("Available")

    else:
        print("Unavailable")

elif colour == "blue":
    available == True

    if size == "s" or size == "m":
        available == True
        print("Available")

    else:
        print("Unavailable")

else:
    print("Nope")

#why doesn't this run?
if colour and size == available:
    output = print("The", colour, "shirt is available in", size)
    print(output)
python
2个回答
1
投票

几个简单的更改将使其起作用。

  1. available == True更改为available = True-首先是比较,其次是分配。
  2. if colour and size == available:不正确。输入coloursize。您只有一个available变量,即TrueFalse。这足以让您检查coloursize是否可用。
#variable inputs
colour = input("What colour shirt? " ).lower()
size = input("What size? " ).lower()

#why do i set available to False?
available = False


if colour == "white":
    if size == "m" or size == "l":
        available = True
        print("Available")

    else:
        print("Unavailable")

elif colour == "blue":
    if size == "s" or size == "m":
        available = True
        print("Available")

    else:
        print("Unavailable")

else:
    print("Nope")

#why doesn't this run?
if available:    
    output = print("The", colour, "shirt is available in", size)
    print(output)

您问为什么一开始将available设置为False。这是使用逻辑的一种很常见的方式。在这种情况下,您假设availableFalse开始。一个合理的假设,因为没有可用的颜色和尺寸选项要多得多。然后,您检查它是否实际上是True,如果是,则在知道它时将其更改为True


0
投票

这是更简洁的示例:

colour = input("What colour shirt?").lower()
size = input("What size? ").lower()

available = False

'''
First we filter out non blue or white shirts
'''
if colour == 'white' or colour == 'blue':
    '''
    Now we get the right sizes
    '''
    if colour == 'white' and size == 'l' or size == 'm':
        available = True
        print('shirt is available')
        print('order is confirmed')
    elif colour == 'blue' and size == 'm' or size == 's':
        available = True
        print('shirt is available')
        print('order is confirmed')
    else:
        print('Sorry, that shirt is unavailable')
else:
    print('Sorry, that shirt is unavailable')

还有其他问题和原始代码:

#variable inputs
colour = input("What colour shirt? " ).lower()
size = input("What size? " ).lower()

#why do i set available to False?
'''
You want to set this to false because you're only going to say a shirt is available when it is.
This way you can't 'forget' to say to you don't have it in stock. It's a safe guard in logic.
'''
available = False


if colour == "white":
    available == True
    if size == "m" or size == "l":
        available == True
        print("Available")

    else:
        print("Unavailable")

elif colour == "blue":
    available == True

    if size == "s" or size == "m":
        available == True
        print("Available")

    else:
        print("Unavailable")

else:
    print("Nope")

#why doesn't this run?
'''
This if block isn't being run because the colour and size variables are strings, 
they aren't changed to mean True or False so the if never returns true.
'''
if colour and size == available:
    output = print("The", colour, "shirt is available in", size)
    print(output)

这是使您的isValid或isTrue变量正确无误的真正常用且简便的方法。您会发现很多可以帮助您的地方。同样,最好查看是否可以在较小的块中编写条件或将类似的块组合在一起。当您大声朗读或自己读回代码时,这将有助于提高代码的可读性和完整性检查。希望能对您有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.