Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying Comparison Operators
#1
Good morrow, guys!

Used this code from a textbook and can't find the error. It seems like something at the beginning already, but I cant figure it out.

Code:
i=10
j=15
if(i==j):
    print("i is equal j")
    else:
    print("i is not equal to j")
    if(i!=i):
        print("i is not equal to j")
        else:
            print("i is equal to j")
            if(i>j)
            print("i is greater than j")
            else:
                print("i is not greater than j")
                if(i<j)
                print("i is less than j")
                if(i>=j)
                print("i is greater than or equal to j")
                else:
                    print("i is neither greater than nor equal to j")
                    if(i<=j)
                    print("i is less than or equal to j")
                    else:
                        print("i is neither less than or equal to j")
Many thanks everyone!
Reply
#2
Please, always post full traceback you get.
Traceback show you where the error is. In this case line 5, where else is indented one-level too much. it should be at the same level as corresponding if.

Also note that there are problems with the logic - e.g. some check sa redundant and also lines 17 and next will never be executed. They are in the else block implying that i < j, so i >= j will never be True
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
#3
Syntax errors are there in code -
1. "if(i==j):" should be "if i==j:"
2. ":" is missing in most of the conditions.
3. Indentation is not correct.

Code with syntax errors corrected -
i=10
j=15
if i==j:
    print("i is equal j")
else:
    print("i is not equal to j")
    if i!=i: # here doesn't look correct logically.
        print("i is not equal to j")
    else:
        print("i is equal to j")
        if i>j:
            print("i is greater than j")
        else:
            print("i is not greater than j")
            if i<j:
                print("i is less than j")
            if i>=j:
                print("i is greater than or equal to j")
            else:
                print("i is neither greater than nor equal to j")
                if i<=j:
                    print("i is less than or equal to j")
                else:
                    print("i is neither less than or equal to j")
The simplified code:
i=10
j=15
		
if i==j:
    print("i is equal j")
else:
    if i>j:
        print("i is greater than j")
    else:
        if i<j:
            print("i is less than j")
Reply
#4
The awesomeness of you guys is unbelievable. Many many thanks indeed!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Use of if - and operators Pedro_Castillo 1 459 Oct-24-2023, 08:33 AM
Last Post: deanhystad
  Mixing Boolean and comparison operators Mark17 3 1,371 Jul-11-2022, 02:20 AM
Last Post: perfringo
  Magic Method Arithmetic Operators ClownPrinceOfCrime 3 2,280 Jan-10-2021, 03:24 PM
Last Post: ndc85430
  Class and Operators in Python rsherry8 1 1,957 May-27-2020, 07:09 PM
Last Post: buran
  Mathematical Operators in String AgileAVS 1 2,343 Mar-04-2020, 04:14 PM
Last Post: Gribouillis
  A doubt with 'in' and 'not in' operators with strings newbieAuggie2019 7 3,518 Oct-23-2019, 03:11 PM
Last Post: perfringo
  understanding exponential and bitwise operators srm 1 2,000 Jun-15-2019, 11:14 AM
Last Post: ThomasL
  please help with this question about using operators to multiply a string? GilesTwigg 3 4,325 Feb-27-2019, 04:13 PM
Last Post: ichabod801
  Understanding compound operators -= NewatCode 3 3,140 Apr-25-2018, 05:03 PM
Last Post: Larz60+
  Pycharm shortcuts and operators don't run AzD 6 7,826 Oct-17-2017, 05:46 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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