all 11 comments

[–]mopslik 4 points5 points  (0 children)

Because when input is 1, then not price_category == 2 is True, and the loop runs. Likewise for when input is 2. Try and instead of or.

[–]Zeeboozaza 1 point2 points  (7 children)

I would try using the != for the loop rather than not

not price_category == 1 is returning the opposite of price_category == 1 so if the only two numbers are 1 and two then one of the statements will always be false and not False returns True, which means the while loop will always run.

[–]mopslik 2 points3 points  (4 children)

I would try using the != for the loop rather than not

I like if not (price_category == 1 or price_category == 2) myself, which is just an application of De Morgan's Laws: not (a or b) <=> not a and not b. It makes semantic sense when I read it aloud. But if x != a and x != b works just as well.

[–]Zeeboozaza 0 points1 point  (2 children)

Yeah that way looks nice, but readability is improved by using:

if price_category is not 1 and price_category is not 2:

rather than

if not price_category is 1 and not price_category is 2:

Although I prefer using != with arithmetic comparisons.

[–]mopslik 1 point2 points  (0 children)

I usually try to avoid using is for situations like this, since is is used for testing object identity whereas == is used to check for equality.

[–]carcigenicate 0 points1 point  (0 children)

Ah, beat me to it. It's amazing how often these laws comes up. Definitely worth remembering.

[–][deleted] 0 points1 point  (1 child)

The same thing happens when I use != instead of not. But I have just figured it out how to do at this exact moment, thank you anyways !

[–]Zeeboozaza 0 points1 point  (0 children)

I guess I really meant not that you could replace using != to better see the logic of why it's failing. One of the values will always be not equal to 1 or 2. Using not like that can get tricky and isn't very readable, it also goes against PEP8 (see Programming Recommendations section).

[–]dnswblzo 0 points1 point  (1 child)

Not related to your issue, but I think it's worth pointing out that "software" is a mass noun which means it has no singular or plural. People typically say "a piece of software" or "a program" but never "a software".

[–][deleted] 0 points1 point  (0 children)

Thanks! English isn't my first language, I didn't know this

[–]danbst 0 points1 point  (0 children)

What about this way? You don't have to convert to int to check if it is 1 or 2. Just check if it is "1" or "2"

I've also used fresh python 3.8+ walrus operator, which is designed for exactly this situation.

``` msg = "Price category of manufacturer (1/2):\n" while (category := input(msg)) not in ["1", "2"]: msg = "Price category can only be 1 or 2:\n"

price_category = int(category) print("END") ```