Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Else statement
#1
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?
Reply
#2
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')
Reply
#3
(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?
Reply
#4
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
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.
Reply
#6
(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.
Reply
#7
(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.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020