r/pythonhelp • • 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?

4 Upvotes

11 comments sorted by

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:

if number == 20:
    x = 0

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.

3

u/[deleted] Oct 04 '21

That was the only way I knew to do it. None of the other assignments in the block covered a scenario that didn't involve the greater than, less than.

3

u/skellious Oct 04 '21

Ah. Sounds like your teacher is at fault then. In that case, full marks from me for working with what you had.

1

u/[deleted] Oct 04 '21

I've had quite a few issues with them so far. One instance they docked me points for using a b and c as variable names, but never said we couldn't use a single letter as the variable.

Another they marked a quiz answer wrong but according to the text they gave us it was right and I had to argue to get it fixed.

3

u/skellious Oct 04 '21

It's difficult because while they are correct that outside of iterators you shouldn't use single letter variable names, they shouldn't punish you if they don't tell you this.

Are you sure there was no style guide provided at any point in the course?

Read pep8 for help styling your code correctly.

Perhaps consider also following a seperate python course to help you.

1

u/[deleted] Oct 04 '21

It was the second overall assignment, first one using variables. I followed the directions. The examples in that particular assignment used a simple letter. Never occurred to me not to do the same.

I stopped after that one, so unless it specifically says to use a simple letter I don't anymore.

1

u/skellious Oct 05 '21

Sorry to hear your teacher has punished you for things he taught you. :(

3

u/cndvcndv Oct 04 '21

I like the equality work around :D

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?

  1. it'll be undefined because you never set the value of x anywhere.

  2. if you fix that, then number is not 20, but you treat it like it is.

  3. you're comparing the value of number, not the value of y.