"If" | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

"If"

So basically I am learning python from a website and ive got to if statement part and after they explained the gave me a homework but i dont understand how to do it and how to write the code Create a function that accepts 3 parameters and checks for equality between any two of them. Create a function that accepts 3 parameters and checks for equality between any two of them. Your function should return True if 2 or more of the parameters are equal, and false is none of them are equal to any of the others. The code should be really simple but i dont know how to write it (I have learned about variables functions and if )

7th Feb 2020, 12:34 PM
kasisX
kasisX - avatar
14 Answers
+ 2
1. This is not a function 2. If they are asking for equality, what is the purpose of <= and >= ? 3. I don' understand the choice of values, why are some of them numbers, and the others booleans? Just use: a = 1 b = 8 c = 4 4. This is how to check multiple equal statements: If a==b or a==c or c==b
7th Feb 2020, 12:59 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 2
kasisX then read the course again, I'm sure it was well explained there. Keep trying until you find the solution, it's best to do it yourself.
7th Feb 2020, 1:02 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 2
Your homework is asking you to write a program so that when someone else gives you 3 numbers, the program tells you if at least 2 of them are the same. First, you need to set up a function like they asked, that accepts 3 numbers. For example: def yourfunction(firstnum, secondnum, thirdnum): Then tell the function what it should do. For example, if firstnum == secondnum, then return what? Otherwise, if firstnum is not the same as secondnum, then what? Since only 2 of the numbers have to match, maybe you could use an “or” statement?
7th Feb 2020, 2:53 PM
Isabel
Isabel - avatar
+ 1
Can you show us your attempt ?
7th Feb 2020, 12:41 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
0
See thats the thing i dont understand how to do it
7th Feb 2020, 1:00 PM
kasisX
kasisX - avatar
0
Ok
7th Feb 2020, 1:02 PM
kasisX
kasisX - avatar
0
Def check(num1, num2, num3,): If num1 == num2 or num2 == num3 or num3 == num1 Print("True") Else: Print("False") check(2, 3, 2) I fleel like there should be 4th one but could this code above work ?
7th Feb 2020, 5:51 PM
kasisX
kasisX - avatar
0
kasisX Yes! This does work. But make sure you double check your indentation (2 spaces at each level). Also, the instructions (like def) need to be all lower case and if statements need to end with “:”. I am very, very bad at these details, but I am learning!
7th Feb 2020, 6:41 PM
Isabel
Isabel - avatar
0
Yes, ty for helping
7th Feb 2020, 6:46 PM
kasisX
kasisX - avatar
0
Did you learn the "set" data structures? If yes then take a look at this: def test_equality(a,b,c): return len({a, b, c}) < 3 If not then something like this will do the job: def test_equality(a, b, c): if a == b or a == c or b ==c:. #those are all the possible cases return True return False
8th Feb 2020, 8:28 AM
MedG
MedG - avatar
0
No i havent
8th Feb 2020, 8:54 AM
kasisX
kasisX - avatar
0
kasisX no worries, you will soon. Anyways, the second methode uses only variable (a,b,c), functions (test_equality) and if statements, things you said have learned. If you need further help plz ask.
8th Feb 2020, 9:24 AM
MedG
MedG - avatar
0
def check_fun ( num1, num2, num3) : If num1==num2 or num1==num3 or num2==num3: return True else: return False
8th Feb 2020, 4:23 PM
Amrit Sood
Amrit Sood - avatar
- 1
Well i was trying like #school starts at 8 Time = 8 Energy =5 Sleepy = False Wakeup = true If time <= 7 and energy <= 5 or sleepy == true: Wakeup = false Print(wakeup) Elif time >=8 and energy <=5 or sleepy == true: Wakeup = true Print(wakeup) I dont understand how to make it if 2 of 3 are equal then do the thing code above is just my guess
7th Feb 2020, 12:55 PM
kasisX
kasisX - avatar