Why am I receiving a SyntaxError: invalid syntax regarding the use of an elif statement?

Huey

I am trying write a code which generates a series of responses: if the user enters 1 number, several numbers or a string instead in order to generate a list of Fibonacci numbers. The code is as follows:

def fib (a, b):
    return a + b

number = int(input("Please write how many Fibonacci numbers you wish to have generated: "))

fibonacci_list = []
for n in range(number):
    if n in [0, 1]:
        fibonacci_list += [1]
        print("The first", number, "Fibonacci number is:", fibonacci_list)
    elif:
        fibonacci_list += [fib(fibonacci_list[n-2], fibonacci_list[n-1])]
        print("The first", number, "Fibonacci numbers are:", fibonacci_list)
    else:
        print('Sorry could not recognise the input')
Joe

You should write a condition for elif too, for example:

elif n in [1,2]:

In your case, i would write the code in this way:

def fib (a, b):
    return a + b

try:
    number = int(input("Please write how many Fibonacci numbers you wish to have generated: "))

    fibonacci_list = []
    for n in range(number):

        if n in [0, 1]:
            fibonacci_list += [1]
            print("The first", number, "Fibonacci number is:", fibonacci_list)
        else:
            fibonacci_list += [fib(fibonacci_list[n-2], fibonacci_list[n-1])]
            print("The first", number, "Fibonacci numbers are:", fibonacci_list)
except:
    print('Sorry could not recognise the input')

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Why am I receiving intermittent duplicate update errors on a MERGE statement, where it shouldn't be possible?

分類Dev

Why am I receiving the following error when subscribed to the patientAllergies observable?

分類Dev

Why am I receiving negative values for my hash function?

分類Dev

Why am I receiving a "thread exiting with uncaught exception” in Android?

分類Dev

Python Ifstatement SyntaxError:Invalid Syntax

分類Dev

Django 1.9 Installation SyntaxError: invalid syntax

分類Dev

invalid syntax after multiple if statement

分類Dev

Why am I getting an Invalid Procedure call or argument on a Range union

分類Dev

Why am I getting "invalid max count" from grep in an alias?

分類Dev

Why am I receiving PLS-00103 when attempting to create this simple function?

分類Dev

Why am I receiving a 500 status when trying to connect with ASANA api?

分類Dev

I am not receiving WebSocket messages (SimpMessagingTemplate)

分類Dev

Why Invalid Syntax in Python Unittest

分類Dev

django different serializer for GET and PUT - SyntaxError: invalid syntax

分類Dev

Why am I not able to use stopwatch.Restart()?

分類Dev

Why am I forced to use !! in a combination of null checks?

分類Dev

Why use omnifaces while I am using primefaces?

分類Dev

Why is my elif being treated as an else statement in my bash script?

分類Dev

Why does f.write overwrites a line after an elif statement?

分類Dev

i am a beginner in Rstudio !! my doublt is regarding = installing devtools

分類Dev

Why can I use initializer syntax with readonly properties

分類Dev

I am trying to use XPath function contains() that has a string in 2 parts but it is throwing an invalid xpath error

分類Dev

clarification regarding an if statement in ksh

分類Dev

Why am I able to use my value constructor even though I don't export it?

分類Dev

{"Error":"Invalid JSON syntax at offset 2"} - receiving this error while trying to get everflow report

分類Dev

Python - if, elif, elif, else Statement Not Working as Expected

分類Dev

Why am I getting an undefined?

分類Dev

Why am I getting this KeyError?

分類Dev

Installing jupyter with Python 3.8 fails with "SyntaxError: invalid syntax" because it's using Python 2.7

Related 関連記事

  1. 1

    Why am I receiving intermittent duplicate update errors on a MERGE statement, where it shouldn't be possible?

  2. 2

    Why am I receiving the following error when subscribed to the patientAllergies observable?

  3. 3

    Why am I receiving negative values for my hash function?

  4. 4

    Why am I receiving a "thread exiting with uncaught exception” in Android?

  5. 5

    Python Ifstatement SyntaxError:Invalid Syntax

  6. 6

    Django 1.9 Installation SyntaxError: invalid syntax

  7. 7

    invalid syntax after multiple if statement

  8. 8

    Why am I getting an Invalid Procedure call or argument on a Range union

  9. 9

    Why am I getting "invalid max count" from grep in an alias?

  10. 10

    Why am I receiving PLS-00103 when attempting to create this simple function?

  11. 11

    Why am I receiving a 500 status when trying to connect with ASANA api?

  12. 12

    I am not receiving WebSocket messages (SimpMessagingTemplate)

  13. 13

    Why Invalid Syntax in Python Unittest

  14. 14

    django different serializer for GET and PUT - SyntaxError: invalid syntax

  15. 15

    Why am I not able to use stopwatch.Restart()?

  16. 16

    Why am I forced to use !! in a combination of null checks?

  17. 17

    Why use omnifaces while I am using primefaces?

  18. 18

    Why is my elif being treated as an else statement in my bash script?

  19. 19

    Why does f.write overwrites a line after an elif statement?

  20. 20

    i am a beginner in Rstudio !! my doublt is regarding = installing devtools

  21. 21

    Why can I use initializer syntax with readonly properties

  22. 22

    I am trying to use XPath function contains() that has a string in 2 parts but it is throwing an invalid xpath error

  23. 23

    clarification regarding an if statement in ksh

  24. 24

    Why am I able to use my value constructor even though I don't export it?

  25. 25

    {"Error":"Invalid JSON syntax at offset 2"} - receiving this error while trying to get everflow report

  26. 26

    Python - if, elif, elif, else Statement Not Working as Expected

  27. 27

    Why am I getting an undefined?

  28. 28

    Why am I getting this KeyError?

  29. 29

    Installing jupyter with Python 3.8 fails with "SyntaxError: invalid syntax" because it's using Python 2.7

ホットタグ

アーカイブ