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
  Pyserial issues with proper loops and binary jttolleson 16 2,588 Nov-02-2023, 08:39 PM
Last Post: deanhystad
Question If, elif, and else statement not working PickleScripts 3 884 Mar-30-2023, 02:53 PM
Last Post: PickleScripts
  Getting proper x,y axis values pyhill00 8 1,635 Jul-29-2022, 06:48 PM
Last Post: pyhill00
  Proper way to do the OR statement? Mark17 5 1,763 Mar-01-2022, 01:54 PM
Last Post: Mark17
  If elif else statement not functioning SamDiggityDog 4 2,647 Jun-03-2020, 12:27 AM
Last Post: SamDiggityDog
  proper use of 'end' in a 'with' statement ccrider27 1 2,049 Mar-18-2020, 10:33 PM
Last Post: buran
  Proper Layout of Code Question TheJax 2 2,190 Feb-08-2020, 06:14 PM
Last Post: TheJax
  Syntax Error (elif statement) Kanashi 0 3,661 Nov-20-2019, 11:29 PM
Last Post: Kanashi
  Unable to do the proper split using re.sub incase of missing data. Karz 1 1,854 Nov-17-2019, 05:58 PM
Last Post: buran
  getopt with tuple not working proper Frank123456 0 1,867 Aug-21-2019, 12:46 PM
Last Post: Frank123456

Forum Jump:

User Panel Messages

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