In this Python tutorial, we will discuss how to fix math domain errors in python. We will check how to fix the error called **valueerror: math domain error** and **valueerror:** **too many values to unpack python**.

## ValueError: math domain error

In python, we will get this error while working with mathematical function in python. This type of error usually occurs when we try to find out the square root of a negative number in python.

**Example:**

```
from math import sqrt
my_number=float(input("Enter number : "))
print("Square root of a number :",sqrt(my_number))
```

After writing the above code, Ones you will print **“ sqrt(my_number) ” **then the error will appear as a “** valueerror: math domain error** ”. Here, this error occurs because we are finding the square root of a negative number.

You can see the below screenshot for this error

To solve this **python math domain error** we can give correct values to math function and also by avoiding negative values as it doesn’t have a real square root.

**Example:**

```
from math import sqrt
my_number=float(input("Enter number : "))
print("Square root of a number :",sqrt(my_number))
```

After writing the above code, Ones you will print **“ sqrt(my_number) ” **then the output will appear as a “

**Square root of a number: 2.449489742783178**”. Here, the error is resolved by giving a positive number.

You can refer to the below screenshot how my error is resolved.

**ValueError: too many values to unpack python**

The **valueerror:** **too many values to unpack** error will be raised during the assignment as the number of list element is more than the number of variable.

**Example:**

```
my_list = [5,10,15,20]
a,b,c = my_list
print(a)
print(b)
print(c)
```

After writing the above code, Ones you will print then the error will appear as a “** valueerror: too many values to unpack”**. Here, this error occurs when we declare more value in the list than the number of variables.

You can see the below screenshot for this error

This **error is solved** by giving an** equal number of objects** for the assigned number of variables. So, we unpacked the elements from the list by assigning the matching number of variables.

**Example:**

```
my_list = [5,10,15]
a,b,c = my_list
print(a)
print(b)
print(c)
```

After writing the above code, Ones you will print then the output will appear as a **“ 5 10 15”**. Here, the number of list elements and the number of variables is matching. So, in this way, we can avoid this error.

You can see the below screenshot how the valueerror is solved

You may like the following Python tutorials:

- Check if a number is a prime Python
- Python pip is not recognized as an internal or external command
- Python convert tuple to list
- What does the percent sign mean in python
- Python built-in functions with examples
- Get current directory Python
- Python if else with examples
- Python TypeError: ‘list’ object is not callable

This is how to fix ValueError in python or **ValueError: math domain error** and **valueerror:** **too many values to unpack python**.

Entrepreneur, Founder, Author, Blogger, Trainer, and more. Check out my profile.