Hi everyone,
I keep running in to this issue when I want to create an Else statement. I think something goes wrong with the indentation, but I could be wrong. I tried looking for proper explanations, but I'm still getting errors.
Something goes wrong after 'Print('Same'). It doesn't jump to the next line. So I gave it an enter. Here is probably where I'm going wrong. But I don't know how to fix it.
# samedifferent
>>>
>>> d1 = 1.11 - 1.10
>>> d2 = 2.11 - 2.10
>>> print('d1 =', d1, ' d2=', d2)
d1 = 0.010000000000000009 d2= 0.009999999999999787
>>> if d1 == d2:
print('Same')
>>> else:
SyntaxError: invalid syntax
Any help of hints?
You are correct that the problem is with indentation. Everything in between if and else must be indented like this:
if d1 == d2:
print('Same')
else:
print ('Different')
(Mar-08-2021, 03:44 PM)BashBedlam Wrote: [ -> ]You are correct that the problem is with indentation. Everything in between if and else must be indented like this:
if d1 == d2:
print('Same')
else:
print ('Different')
Hi Bash,
that's kind of the problem. After the If statement, it won't indent for the Else statement. Am I missing something?
first of all I would recommend to write and save your code in py file, not in python interactive shell
if you insist on using shell - when you hit enter after the
if
line, indent the
print
, then hit again and you can enter
else
. If the code allow for indented block you need 2 enters to execute the whole block.
Python 3.7.10 (default, Feb 20 2021, 21:21:24)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> spam = 1
>>> eggs = 2
>>> if spam == eggs:
... print("same")
... else:
... print('different')
...
different
You have to un-indent if you are using the IDLE shell.
Output:
>>> if 5 > 3:
print('hi')
else:
print('ho')
hi
After entering "print('hi')" the next line was indented to the same level. I just hit the backspace key end entered "else:"
Your problem is you completed the if statement before entering else. You do not want to see ">>>" because that means you are done with the expression.
(Mar-08-2021, 04:00 PM)buran Wrote: [ -> ]first of all I would recommend to write and save your code in py file, not in python interactive shell
if you insist on using shell - when you hit enter after the if
line, indent the print
, then hit again and you can enter else
. If the code allow for indented block you need 2 enters to execute the whole block.
Python 3.7.10 (default, Feb 20 2021, 21:21:24)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> spam = 1
>>> eggs = 2
>>> if spam == eggs:
... print("same")
... else:
... print('different')
...
different
Thank you for the explanation. I'll give that a go.
(Mar-08-2021, 04:04 PM)deanhystad Wrote: [ -> ]You have to un-indent if you are using the IDLE shell.
Output:
>>> if 5 > 3:
print('hi')
else:
print('ho')
hi
After entering "print('hi')" the next line was indented to the same level. I just hit the backspace key end entered "else:"
Your problem is you completed the if statement before entering else. You do not want to see ">>>" because that means you are done with the expression.
Thank you for your responds. I'm looking into it.