Syntaxerror: invalid decimal literal

The syntaxerror: invalid decimal literal is an error message you’ll probably experience while working with Python.

If you’re having a hard time trying to fix this error. Then, keep on reading!

In this article, we’ll walk you through how you will resolve the invalid decimal literal, a SyntaxError in Python.

Let’s start to explore together what this error is all about and how to fix it.

What is “syntaxerror invalid decimal literal”?

The error message syntaxerror: invalid decimal literal occurs when you are trying to use an invalid decimal literal in a Python expression.

In a simple understanding, this error is typically raised we you declare a variable with a name that begins with a number.

For example:

500_steps = range(0, 500)

Output:

 File "C:\Users\pies-pc2\PycharmProjects\pythonProject\main.py", line 1
    500_steps = range(0, 500)
       ^
SyntaxError: invalid decimal literal

What is literal means?

In Pythion, literals are values that include:

✔ Numbers

✔ Strings

✔ Booleans

For example:

string = 'Itsourcecode'

integer = 500

boolean = True or False

On the other hand, wherever side of an operator it is, any literal that begins with a number is a decimal literal.

Meanwhile, a variable should start with a letter or an underscore because it cannot start with a digit or number.

Note: Decimal literal cannot contain any alphabet letters.

Why does “invalid decimal literal” error occurs in Python?

The syntaxerror invalid decimal literal error occurs in Python when the interpreter encounters a line of code that violates the rules of the Python syntax.

This error can happen under the following scenario:

👎 When a decimal value contains letters or when an identifier (variable or function name) starts with a number.

For instance, if you declare a variable with a name that starts with a digit, you will get this error.

How to fix “syntaxerror: invalid decimal literal”?

To fix the syntaxerror invalid decimal literal error in Python, ensure that you start or begin the variable name with a letter or an underscore, as variable names cannot begin with numbers.

Solution 1: Move digits to the end of the variable

For example:

Incorrect code

500_steps = range(0, 500)
for step in steps_500:
    print(step)

The variable name 500_steps starts with a digit, which is not allowed in Python. To fix this error, we move the number to the end of the variable name, to begin with a letter or an underscore.

Corrected code

steps_500 = range(0, 500)
for step in steps_500:
    print(step)

You can also use the following code:

fivehundred_steps = range(0, 500)
for step in fivehundred_steps:
    print(step)

Solution 2: Remove letters from a decimal value

For example:

Incorrect code

amount = 500.99€

The decimal value 500.99€ contains a letter, which is not allowed in Python. To fix this error, we removed the letter from the decimal value.

Corrected code

amount = 500.99

Alternatively, you use a string rather than using a decimal value.

amount = '500.99'

Solution 3: Declare a string

Another way to fix this error is to declare a string by wrapping the value in quotes or double quotes.

For example:

amount = '500.99 €'
print(amount)

Output:

500.99 €

Solution 4: Use a different data type

You can simply change the data type to a dictionary and store the value and currency separately.

For example:

amount = {'amount': 500.99, 'currency': '€'}
print(f"The amount is {amount['amount']}{amount['currency']}")

Output:

The amount is 500.99€

Conclusion

In conclusion, the error message syntaxerror: invalid decimal literal occurs when you are trying to use an invalid decimal literal in a Python expression.

To fix the syntaxerror invalid decimal literal error in Python, ensure that you start or begin the variable name with a letter or an underscore, as variable names cannot begin with numbers.

This error message is easy to fix and understand, by executing the solutions above, you can master this SyntaxError with the help of this guide.

You could also check out other SyntaxError articles that may help you in the future if you encounter them.

We are hoping that this article helps you fix the error. Thank you for reading itsourcecoders 😊

Leave a Comment