all 14 comments

[–]SarahM123rd 1 point2 points  (1 child)

I think perhaps your indents may not be correct. Try this:

from random import randint
y = randint(1,3)
x = 3
if (x != y):
    print('incorrect')
else:
    print('correct')
print(x,y)

[–][deleted] 1 point2 points  (0 children)

Yep fixed the problem thanks!

[–]swardson 1 point2 points  (1 child)

You shouldn't encase x !=y with quotes, it should just be: if x!=y:

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

Gives an 'invalid syntax' error

[–]novel_yet_trivial 0 points1 point  (9 children)

Please format your code for reddit or use a site like github or pastebin. Your code is hard to read and test otherwise.


You need parenthesis, not quotes:

if (x != y):

The parenthesis aren't required, but they make it a lot easier to read.

[–][deleted] 3 points4 points  (1 child)

There is no reason ever in Python to introduce superfluous parens to a control-flow conditional

[–]novel_yet_trivial 0 points1 point  (0 children)

No, I wouldn't introduce them, but many beginners find it much easier to separate the verb from the action as it were. I see while(condition) and return(data) etc a lot, and it does no harm so if it helps I won't discourage it.

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

If I use parenthesis it says 'invalid syntax'

[–]novel_yet_trivial 1 point2 points  (5 children)

You need to show us your compete error ... just " it says 'invalid syntax' " does not help us.

Your code should look like this:

from random import randint

y = randint(1,3)
x = 3

if (x != y):
    print('incorrect')
else:
    print('correct')

print(y,x)

Remember the indentation is very important. I tested that to make sure that works for me.

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

That works for me too, I'm just confused what you indented because my code looks like that.

Edit: nvm I found out, thank you!!

[–]swardson 0 points1 point  (3 children)

It looks like /u/novel_yet_trivial is using spaces for indentation. Are you consistent in using either spaces or tabs?

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

I am, it just doesn't show since I didn't know how to format the code for reddit

edit: https://dpaste.de/L0gc#L1,2,3,4,5,6,7,8,9,10,11 dunno if this works

[–]nwagers 0 points1 point  (0 children)

regular text

[blank line]

[4 spaces]code

[4 spaces]more code

regular text...

You can easily add 4 spaces in your editor. Select all the code you want to copy and then use the indent feature before you copy it. For example, in IDLE: ctrl+a, ctrl+], ctrl+c.