The objective of my task is to allow the user to enter a series of colours (maximum and minimum of 4, and have to be within 5 set colours). I'm supposed to write code that continuously prompts the user if what they've entered doesn't meet the criteria. And also enter a width and height between the numbers 2-8 (including 2 and 8). Am I able to use the & symbol when creating decision statements? I'm not sure if it is fact or not but I assumed it was a possibility due to the simultaneous assignment I set for variables :

width, height

I've tried everything I can, and I feel the key part I'm missing is membership checking what the user has entered to what "set colour list". My code is below:

def main():
    colourslst = ["red", "green", "blue", "magenta", "cyan"]
    width, height = eval(input("Enter the width and height of patches: "))
    while True:
        [B]if width & height > 8 and width & height < 2:[/B]
            width, height = eval(input("Enter the width and height of patches: "))
            colour = input("Please enter the desired four colours: ")
        [B]if len(colour.split()) < 4 and colour.split() != colourslst:[/B]
            colour = input("Please enter the desired four colours: ")
            colourslst = colour.split()

I know using if and else twice is an issue, however that is irrilevant right now as I am only interested in the part that's made bold at the moment until I solve the decision functions.


Recommended Answers

All 4 Replies


if width & height > 8 and width & height < 2:


if 8 < width < 2 and 8 < height < 2:

But do not expect it to become True before hell freezes over ;)

Do you want to say:

if 2 < width < 8 and 2 < height < 8:

You don't ever exit the "while True" loop.

and colour.split() != colourslst:
This will never be true because one has four colors and one has 5 colors (excuse me, 'colours'). You definitely want to add some print statements to see what is happening in the program.

@Tony - I'm saying:

If width and height is less than 2 or greater than eight:
<repeat body>

What you've written isn't clear as to what I'm saying, what you've written says to me:
if 2 is less than the width and if width is less than 8, AND, if 2 is less than the height and if height is less than 8:
<repeat body>

I thought you could only do one at a time anyway? Or is what you've written possible but with just more extensive conditions?

I appreciate colour.split() will never be colourslst as one has 4 in the list and the other has 5. However, I attempted:

if len(colour.split()) < 4 and colour.split() in colourslst:

Which is somewhat closer to what I want, but of course wouldn't provide me with the boolean expression I want for the body. Hence why I've put "membership check" in the title, I feel I'm stuck in a rut on this one and can't see beyond any boundaries.

The reason why I've over-written colourslst with colour.split() is because the colours that the user has entered will be called upon later on to change colours of shapes.

So using the "&" symbol is not possible and what tony has suggested is what I want? And as for adding print statements, I don't feel theres any need to add print statement as all I want is for body to be repeated if conditions aren't met but I'm clearly setting wrong conditions. The result should be repetitive body's if input is invalid

You can use sets for this

and colour.split() in colourslst:

colours_set = set(colourslst)
input_set = set(colour.split())
if input_set.issubset(colours_set):

The result should be repetitive body's if input is invalid

Use something like this which will repeat if any of the conditions are false, but I think you want to repeat each input item individually until it is correct and then go on to the next input. The way you are doing it, if one input item is incorrect you have to re-enter all items.

valid = False
while not valid:
    ## enter input here
    if len(colours.split()) == 4:
        if input_set.issubset(colours_set):
            if 2 < width < 8 and 2 < height < 8:
                valid = True
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.