衬衫订购程序

时间:2019-11-20 23:40:49

标签: python

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

The task

我感到困惑的是为什么我的 if 末尾语句无法运行,并且我不确定布尔如何工作,为什么必须将False设置为False才能工作,以及然后将其余设置为True。为什么我不能仅将True设置为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)

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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变量正确的真正常用且简便的方法。您会发现很多可以帮助您的地方。同样,最好查看是否可以在较小的块中编写条件或将类似的块组合在一起。当您大声朗读或自己读回代码时,这将有助于提高代码的可读性和完整性检查。希望这会有所帮助!