-1

I have a problem with the min function. I'm trying to use it in an if statement in combination with the and operator. An easy example would be:

if min(1,2,3) == 2 and 3:
    print(1)

My output here is actually 1 because it seems that the "==" checks the list rather than the value of min(). I tried to assign min(1,2,3) to a varible but it still did the same. I could obviously write my own min() function. But before I do this I would like to know if there's a way for my code to compare the the actual lowest value of the min() function with my 2 and 3 rather than comparing it with the arguments within the brackets.

Thanks for reading!

Edit: I forgot to mention that I searched for a solution online already but I couldn't figure it out

Another edit: I'll try to explain it differently: I'm basically calculating 3 different Values and assign them to Variables. I use the lowest of them for further calculations this is why I use the min function in the first Place. Afterwars I want create a list where I store the information which of the three variables was the lowest for the current calculation. (I'm trying to do this with multiple if statements because all 3 variables could have the same Value). So what I do is:

list = []
if min(v1, v2, v3) == v1:
    list.append(1)

Why I append one is irrelevant but the problem is, that it alway appends one even though v1 is not the lowest value of the given three variables. So the if statement is alway executed. Sorry that it wasn't clear in the first place.

solution: so I just read the comments and one said (min(1,2,3) == 1) could work and it actually did. I don't really understand why as I'm fairly new to programming but if it works I'm happy. So thank you very much!

khooi123
  • 1
  • 2
  • 1
    I don't understand what condition you're trying to express? – Stef Jan 22 '22 at 12:33
  • would `if min(1,2,3) in [2,3]:` do what you want? – Stef Jan 22 '22 at 12:34
  • It's really hard to understand what you want. Note that the minimum of [1,2,3] is always 1. – Stef Jan 22 '22 at 12:34
  • That statment is **not** the same as `(min(1,2,3) == 2) or (min(1,2,3) == 3)`, if that is what you are trying to write – azelcer Jan 22 '22 at 12:36
  • whats your expected output? – XxJames07- Jan 22 '22 at 12:42
  • `min` does "just" return the minimum value of the given input. The smallest value between `1`, `2` and `3` is `1`, so `min(1,2,3)` is `1`. You [shouldn't get any output](https://ideone.com/Gs7bmK) for the code you've shown, because the if-condition is false because `1` doesn't equal `2`. Can you clearly explain what you're trying to do and what do you expect or want `min` to return? Can you also add a few examples showing different inputs and outputs, to help clarify your explanation? – Bernhard Barker Jan 22 '22 at 12:43
  • Hi, Bernhard Barker. It gave me this output though. It just worked with brackets. – khooi123 Jan 22 '22 at 13:06
  • Is this what you're trying to do: `list.append(min(v1, v2, v3))` or `x = min(v1, v2, v3)`? Then `x` would be the lowest of them that you can use for future calculations, as you say. You just need to assign the result of `min` to something, or pass it to another function, if you want to actually use the result. You should only put it in an if-statement if you *only* care about the minimum in as far as you want to check whether it's equal to something (or between something, or some other condition). – Bernhard Barker Jan 22 '22 at 13:20

1 Answers1

0

I`m not sure what you asked for.

If you want to change to '==' sign to something else here is a way:

if min(1,2,3) in [2,3]:
    print(1)
Tal Folkman
  • 2,368
  • 1
  • 7
  • 21