Program: shirt order - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Program: shirt order (/thread-7618.html) |
Program: shirt order - Truman - Jan-17-2018 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. RE: Program: shirt order - Larz60+ - Jan-18-2018 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: RE: Program: shirt order - Truman - Jan-18-2018 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!") breakI 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? RE: Program: shirt order - Larz60+ - Jan-18-2018 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", rightThat 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() RE: Program: shirt order - Truman - Jan-18-2018 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. :) RE: Program: shirt order - Larz60+ - Jan-19-2018 the while loop should be: while not available: |