Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Program: shirt order
#1
Instructions:
First get input for color and size
White has sizes L, M
Blue has sizes M, S
print avaiable 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 avaiable*
# [ ] create shirt order using nested if

my code:
color = input("Choose color between white and blue: ")
size = input("Choose size: ")
available = False
while True:
    if color == "white":
        if size == "M" or size == "L":
            print("available")
            available = True
          
        else:
            print("unavailable")
    elif color == "blue":
        if size == "M" or size == "S":
            print("available")
        else:
            print("unavailable")
    else:
        print("Please choose the correct color!")
whatever I add outcome is always infinite loop. I would appreciate if you give me some ideas how to solve this. For example, I do not understand the function of available = False/ True.
Reply
#2
first, change your while loop to test available:
while not available:
second, you set available = True if color is white and size = 'M' or 'L'
but not if color == 'blue' and size == 'M' or size == 'S'

Add the second available = True, and fix your while loop and you should be good.
hint: you can check more than one condition in if statement, like:
if animal barks and animal has tail:
Reply
#3
color = input("Choose color between white and blue: ")
size = input("Choose size: ")
available = False
while not available:
    if color == "white":
        if size == "M" or size == "L":
            print("available")
            available = True
           
        else:
            print("unavailable")
            break
    elif color == "blue":
        if size == "M" or size == "S":
            print("available")
            available = True
        else:
            print("unavailable")
            break
    else:
        print("Please choose the correct color!")
        break
I don't understand why Python tags don't work now.
Anyway, I have several questions regarding the code now.
1. this part
available = False
while not available:
"while not available" in this case means True since we have "available = False", right? I still don't understand why we need this in exactly this way? Can we do the opposite? Set available to True...

2. When adding some third color I first receive Choose size input. Is there any way to avoid this? To get "Please choose the correct color!" immediately if I, for example, type in green colour?
Reply
#4
since available is True, while available in this case evaluates to while True.
But since it's a variable, the same holds for available = False, i.e. 'while False'

This gives you control over how the while loop interprets the condition.
Change the variable, you have changed the condition.

Quote: while not available" in this case means True since we have "available = False", right
That is correct! so the condition 'while not available' means I want to continue this loop while 'not available' is True.

Quote:When adding some third color I first receive Choose size input. Is there any way to avoid this? To get "Please choose the correct color!" immediately if I, for example, type in green colour?

Yes, you're in control, you can do whatever you wish!

In this case you want to use a function:
example:

def choose_item():
    color = size = None
    color = input("Choose color between white and blue: ")
    if color != 'quit':
        size = input("Choose size: ")
    return color, size


def check_if_available(color, size):
    if  ((color == 'white' and
        (size == 'M' or size == 'L')) or
        (color == 'blue' and
        (size == 'M' or size == 'S'))):
        return True
    else:
        return False

def myControl():
    while True:
        color, size = choose_item()
        if color == 'quit':
            break
        if check_if_available(color, size):
            print('Available')
        else:
            print('Not available')

if __name__ == '__main__':
    myControl()
Reply
#5
I checked now the code with
available = True
while available:

and it also works the same way.
Your answer to my second question is a bit way out of my league for now. :)
edit: your solution after giving color and size asks again "Choose color between white and blue:". It's asking infinitely. :)
Reply
#6
the while loop should be:
while not available:
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020