r/pythonhelp • u/[deleted] • Oct 04 '21
SOLVED Please teach me since my "teacher" won't.
I have already gotten this answer to a quiz wrong and there's no changing the grade. So there's that. But this was the question.
Write an if statement that assigns 0 to x if y is equal to 20.
My answer was
if number > 19 and number < 21:
print('x=0')
Obviously that was wrong. Now I asked what it should have looked like, since I clearly don't know, and all they told me was that
== <-- double-equal sign is equality operator
= <-- single equal sign is the assignment operator
So what should it look like?
3
3
u/MT1961 Oct 04 '21
Hm. Not sure what your teacher has taught you. What he was looking for was something like:
if number == 20: x = 0
Seems sorta silly. If he was looking for an inline comparison, which I doubt, you could have done this:
x = 0 if y == 0 else x
Which compares y to 0 (== means compare and check if equal to) and sets x to 0 in that case. The else part sets x to x (in other words, doesn't change it) if y is not equal to 0.
1
u/xelf Oct 04 '21 edited Oct 04 '21
So let's say that number is 19.5.
What will x's value be after your code?
it'll be undefined because you never set the value of x anywhere.
if you fix that, then number is not 20, but you treat it like it is.
you're comparing the value of number, not the value of y.
3
u/skellious Oct 04 '21
You have just printed the statement "X=0"
You have not set X equal to 0.
Also why are you checking if the number is less than or more than? Check if it is equal like this:
No need to print anything.
The first line checks if number is 20
The second assigns 0 to x
As your teacher said. One = sets the left side equal to the right side. Two of them ( == ) checks if the left side and right side are equal and returns True or False.