Python Forum
Proper use of if..elif..else statement
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Proper use of if..elif..else statement
#1
I'm a retired software engineer who has programmed in a large number of different languages, but I am new to Python. As my first effort at writing a Python script, I started working on a program to computer Fibonacci numbers. My initial code is shown below.
def Fibonacci():
    number = 3
    f1 = 1
    f2 = 1
    if number == 1:
        print (f1)
        elif number = 2:
            print (f2)
        else:
            n = 3
            while n <= number:
                f3 = f1 + f2
                f1 = f2
                f2 = f3
                print (f3)
                n = n + 1
However, I am getting a syntax error at the beginning of the "elif" line. Looking in the Python Language Reference showed by the syntax, but there were no examples I could find of actually using this structure. Could you please tell how to rework that elif statement so I do not get a syntax error? Thanks!

BTW, the code I pasted in was properly indented (I think), but I notice when viewed in the forum, it's all squished to the left.
Reply
#2
Essentially, this is an identation error. If/elif/else statements in Python
have the following syntax:

if statement1:
    ... some code
elif statement2:
    .... some code
else:
    .... some code
So, you need to fix identations in your code, e.g.
def Fibonacci():
    number = 3
    f1 = 1
    f2 = 1
    if number == 1:
        print (f1)
    elif number == 2: # (NOTE: = is an assignment operator; == is used for comparison)
        print (f2)
    else:
        n = 3
        while n <= number:
            f3 = f1 + f2
            f1 = f2
            f2 = f3
            print (f3)
            n = n + 1
Reply
#3
Thanks for the help. The "number = 2" was really a typo. I know it should have been "==".
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  If elif else statement not functioning SamDiggityDog 4 853 Jun-03-2020, 12:27 AM
Last Post: SamDiggityDog
  proper use of 'end' in a 'with' statement ccrider27 1 743 Mar-18-2020, 10:33 PM
Last Post: buran
  Proper Layout of Code Question TheJax 2 814 Feb-08-2020, 06:14 PM
Last Post: TheJax
  Syntax Error (elif statement) Kanashi 0 2,220 Nov-20-2019, 11:29 PM
Last Post: Kanashi
  Unable to do the proper split using re.sub incase of missing data. Karz 1 608 Nov-17-2019, 05:58 PM
Last Post: buran
  getopt with tuple not working proper Frank123456 0 672 Aug-21-2019, 12:46 PM
Last Post: Frank123456
  Whats the right way to refactor this Big if/elif/elif ? pitosalas 1 806 Jul-28-2019, 05:52 PM
Last Post: ichabod801
  proper syntax for itertuples? ilcaa72 1 896 Jun-06-2019, 02:41 AM
Last Post: scidam
  Problem with elif statement Haddal99 2 900 May-20-2019, 09:26 AM
Last Post: avorane
  if-elif-else statement not executing laila1a 4 1,265 Jan-05-2019, 02:22 PM
Last Post: buran

Forum Jump:

User Panel Messages

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