{"id":29354,"date":"2021-02-23T10:22:39","date_gmt":"2021-02-23T18:22:39","guid":{"rendered":"https:\/\/careerkarma.com\/blog\/?p=29354"},"modified":"2023-12-01T04:08:59","modified_gmt":"2023-12-01T12:08:59","slug":"python-valueerror-math-domain-error","status":"publish","type":"post","link":"https:\/\/careerkarma.com\/blog\/python-valueerror-math-domain-error\/","title":{"rendered":"Python ValueError: math domain error Solution"},"content":{"rendered":"\n<p>In mathematics, there are operations which do not work on negative numbers or zero numbers. Consider the square root, for example. You cannot find the square root of a negative number. Python recognizes that not all operations work with negative or zero numbers.<br><\/p>\n\n\n\n<p>Python will raise an error when you try to use a negative number on an operation that does not support one. In this guide, we\u2019re going to talk about the cause of the <code>ValueError: math domain error<\/code>. Toward the end of the guide, we\u2019ll walk through a solution to this issue.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">ValueError: math domain error<\/h2>\n\n\n\n<p>The Python <code>ValueError: math domain error<\/code> is raised when you use a number that is not supported by a mathematical operation. This error is commonly raised with the <code>sqrt()<\/code> method and the <code>log()<\/code> method.<br><\/p>\n\n\n\n<p>The ValueError is a type of error that indicates you are performing a mathematical operation on a value that does not work with that operation. In the case of the \u201cmath domain error\u201d, we are using a negative number or a zero number where we should not be.<br><\/p>\n\n\n\n<p>Let\u2019s walk through an example of the <code>ValueError: math domain error<\/code> issue in action.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">An Example Scenario<\/h2>\n\n\n\n<p>We are building a program that calculates the square root of a given number. This program is designed to help students revise their knowledge of square roots.<br><\/p>\n\n\n\n<p>Let\u2019s write a program that calculates the square root of a given number. We will start by importing the math library that we need to calculate a square root:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>import math<\/pre><\/div>\n\n\n\n<p>Next, we\u2019re going to collect a number from the user:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>number = input(&quot;Try solving the problem first using pencil and paper. Then, insert the number whose square root you want to verify: &quot;)<\/pre><\/div>\n\n\n\n<p>We prompt the user to try finding the answer themselves, as our program is designed to help people check their answers. Next, we\u2019re going to find the square root of the value the user inserts:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>answer = math.sqrt(int(number))<\/pre><\/div>\n\n\n\n<p>We convert the value of \u201cnumber\u201d, which stores the number whose square root the user wants to find, into an integer. This is necessary because the <code>input()<\/code> method, which we used to collect the aforementioned number, returns a string. We cannot find the square root of a string value.<br><\/p>\n\n\n\n<p>Finally, let&#8217;s print the answer to the console:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>print(&quot;The square root of {} is {}.&quot;.format(number, answer))<\/pre><\/div>\n\n\n\n<p>We use a <code>format()<\/code> statement to add numbers to our string. Our string will show:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>&quot;The square root of [Number user inserted] is [The square root our program calculated]&quot;<\/pre><\/div>\n\n\n\n<p>Let\u2019s test our program with a negative number:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>Try solving the problem first using pencil and paper. Then, insert the number whose square root you want to verify: -16\nTraceback (most recent call last):\n  File &quot;test.py&quot;, line 5, in &lt;module&gt;\n\tanswer = math.sqrt(int(number))\nValueError: math domain error<\/pre><\/div>\n\n\n\n<p>We inserted the value <code>-16<\/code> into our program. Our code returned an error.<br><\/p>\n\n\n\n<p>Let&#8217;s fix this error.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The Solution<\/h2>\n\n\n\n<p>To fix this error, we need to prompt the user that you cannot calculate the square root of a negative number before we execute the <code>math.sqrt()<\/code> function.<br><\/p>\n\n\n\n<p>Let\u2019s revise our code to make this happen:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>import math\n\nnumber = input(&quot;Try solving the problem first using pencil and paper. Then, insert the number whose square root you want to verify: &quot;)\n\nif int(number) &gt;= 0:\nanswer = math.sqrt(int(number))\nprint(&quot;The square root of {} is {}.&quot;.format(number, answer))\nelse:\n\tprint(&quot;You cannot find the square root of a number less than 0.&quot;)<\/pre><\/div>\n\n\n\n<p>We use an <code>if<\/code> statement to check if the number the user inserts into the program is equal to or greater than zero. If the number meets this criterion, the contents of the <code>if<\/code> statement run. Otherwise, the <code>else<\/code> statement executes, presenting us with a message that we have inserted an invalid number.<br><\/p>\n\n\n\n<p>Let\u2019s run our program again. Our program returns:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>Try solving the problem first using pencil and paper. Then, insert the number whose square root you want to verify: -16\nYou cannot find the square root of a number less than 0.<\/pre><\/div>\n\n\n\n<p>Our code works successfully.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>The <code>ValueError: math domain error<\/code> is raised when you perform a mathematical function on a negative or zero number which cannot be computed. To solve this error, make sure you are using a valid number for the mathematical function you are using.<br><\/p>\n\n\n\n<p>If you want to learn more about coding in Python, check out our <a href=\"https:\/\/careerkarma.com\/blog\/how-to-learn-python\/\">How to Learn Python guide<\/a>. This guide contains a number of learning resources, courses, and books designed for people who are learning the Python programming language.<\/p>\n","protected":false},"excerpt":{"rendered":"In mathematics, there are operations which do not work on negative numbers or zero numbers. Consider the square root, for example. You cannot find the square root of a negative number. Python recognizes that not all operations work with negative or zero numbers. Python will raise an error when you try to use a negative&hellip;","protected":false},"author":240,"featured_media":11205,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[16578],"tags":[],"class_list":{"0":"post-29354","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-python"},"acf":{"post_sub_title":"","sprint_id":"","query_class":"Python","school_sft":"","parent_sft":"","school_privacy_policy":"","has_review":null,"is_sponser_post":"","is_guest_post":""},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.0 (Yoast SEO v27.0) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Python ValueError: math domain error Explanation and Solution<\/title>\n<meta name=\"description\" content=\"On Career Karma, learn how to solve the Python ValueError: math domain error.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/careerkarma.com\/blog\/python-valueerror-math-domain-error\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python ValueError: math domain error Solution\" \/>\n<meta property=\"og:description\" content=\"On Career Karma, learn how to solve the Python ValueError: math domain error.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/careerkarma.com\/blog\/python-valueerror-math-domain-error\/\" \/>\n<meta property=\"og:site_name\" content=\"Career Karma\" \/>\n<meta property=\"article:publisher\" content=\"http:\/\/facebook.com\/careerkarmaapp\" \/>\n<meta property=\"article:published_time\" content=\"2021-02-23T18:22:39+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-01T12:08:59+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/coding-computer-data-depth-of-field-577585.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1000\" \/>\n\t<meta property=\"og:image:height\" content=\"750\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"James Gallagher\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@career_karma\" \/>\n<meta name=\"twitter:site\" content=\"@career_karma\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"James Gallagher\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-valueerror-math-domain-error\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-valueerror-math-domain-error\/\"},\"author\":{\"name\":\"James Gallagher\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\"},\"headline\":\"Python ValueError: math domain error Solution\",\"datePublished\":\"2021-02-23T18:22:39+00:00\",\"dateModified\":\"2023-12-01T12:08:59+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-valueerror-math-domain-error\/\"},\"wordCount\":571,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-valueerror-math-domain-error\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/coding-computer-data-depth-of-field-577585.jpg\",\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/careerkarma.com\/blog\/python-valueerror-math-domain-error\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-valueerror-math-domain-error\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/python-valueerror-math-domain-error\/\",\"name\":\"Python ValueError: math domain error Explanation and Solution\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-valueerror-math-domain-error\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-valueerror-math-domain-error\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/coding-computer-data-depth-of-field-577585.jpg\",\"datePublished\":\"2021-02-23T18:22:39+00:00\",\"dateModified\":\"2023-12-01T12:08:59+00:00\",\"author\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\"},\"description\":\"On Career Karma, learn how to solve the Python ValueError: math domain error.\",\"breadcrumb\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-valueerror-math-domain-error\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/careerkarma.com\/blog\/python-valueerror-math-domain-error\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-valueerror-math-domain-error\/#primaryimage\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/coding-computer-data-depth-of-field-577585.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/coding-computer-data-depth-of-field-577585.jpg\",\"width\":1000,\"height\":750,\"caption\":\"We\u2019ll cover everything from courses to career paths for each degree program in this network administration degree guide.\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-valueerror-math-domain-error\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\/\/careerkarma.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python\",\"item\":\"https:\/\/careerkarma.com\/blog\/python\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Python ValueError: math domain error Solution\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\",\"url\":\"https:\/\/careerkarma.com\/blog\/\",\"name\":\"Career Karma\",\"description\":\"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/careerkarma.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\",\"name\":\"James Gallagher\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg\",\"caption\":\"James Gallagher\"},\"description\":\"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.\",\"url\":\"https:\/\/careerkarma.com\/blog\/author\/jamesgallagher\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Python ValueError: math domain error Explanation and Solution","description":"On Career Karma, learn how to solve the Python ValueError: math domain error.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/careerkarma.com\/blog\/python-valueerror-math-domain-error\/","og_locale":"en_US","og_type":"article","og_title":"Python ValueError: math domain error Solution","og_description":"On Career Karma, learn how to solve the Python ValueError: math domain error.","og_url":"https:\/\/careerkarma.com\/blog\/python-valueerror-math-domain-error\/","og_site_name":"Career Karma","article_publisher":"http:\/\/facebook.com\/careerkarmaapp","article_published_time":"2021-02-23T18:22:39+00:00","article_modified_time":"2023-12-01T12:08:59+00:00","og_image":[{"width":1000,"height":750,"url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/coding-computer-data-depth-of-field-577585.jpg","type":"image\/jpeg"}],"author":"James Gallagher","twitter_card":"summary_large_image","twitter_creator":"@career_karma","twitter_site":"@career_karma","twitter_misc":{"Written by":"James Gallagher","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/careerkarma.com\/blog\/python-valueerror-math-domain-error\/#article","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/python-valueerror-math-domain-error\/"},"author":{"name":"James Gallagher","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"headline":"Python ValueError: math domain error Solution","datePublished":"2021-02-23T18:22:39+00:00","dateModified":"2023-12-01T12:08:59+00:00","mainEntityOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/python-valueerror-math-domain-error\/"},"wordCount":571,"commentCount":0,"image":{"@id":"https:\/\/careerkarma.com\/blog\/python-valueerror-math-domain-error\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/coding-computer-data-depth-of-field-577585.jpg","articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/careerkarma.com\/blog\/python-valueerror-math-domain-error\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/careerkarma.com\/blog\/python-valueerror-math-domain-error\/","url":"https:\/\/careerkarma.com\/blog\/python-valueerror-math-domain-error\/","name":"Python ValueError: math domain error Explanation and Solution","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/python-valueerror-math-domain-error\/#primaryimage"},"image":{"@id":"https:\/\/careerkarma.com\/blog\/python-valueerror-math-domain-error\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/coding-computer-data-depth-of-field-577585.jpg","datePublished":"2021-02-23T18:22:39+00:00","dateModified":"2023-12-01T12:08:59+00:00","author":{"@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"description":"On Career Karma, learn how to solve the Python ValueError: math domain error.","breadcrumb":{"@id":"https:\/\/careerkarma.com\/blog\/python-valueerror-math-domain-error\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/careerkarma.com\/blog\/python-valueerror-math-domain-error\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/python-valueerror-math-domain-error\/#primaryimage","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/coding-computer-data-depth-of-field-577585.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/coding-computer-data-depth-of-field-577585.jpg","width":1000,"height":750,"caption":"We\u2019ll cover everything from courses to career paths for each degree program in this network administration degree guide."},{"@type":"BreadcrumbList","@id":"https:\/\/careerkarma.com\/blog\/python-valueerror-math-domain-error\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/careerkarma.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Python","item":"https:\/\/careerkarma.com\/blog\/python\/"},{"@type":"ListItem","position":3,"name":"Python ValueError: math domain error Solution"}]},{"@type":"WebSite","@id":"https:\/\/careerkarma.com\/blog\/#website","url":"https:\/\/careerkarma.com\/blog\/","name":"Career Karma","description":"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/careerkarma.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94","name":"James Gallagher","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","caption":"James Gallagher"},"description":"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.","url":"https:\/\/careerkarma.com\/blog\/author\/jamesgallagher\/"}]}},"_links":{"self":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/29354","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/users\/240"}],"replies":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/comments?post=29354"}],"version-history":[{"count":0,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/29354\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media\/11205"}],"wp:attachment":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media?parent=29354"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/categories?post=29354"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/tags?post=29354"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}