{"id":35478,"date":"2021-06-08T09:51:39","date_gmt":"2021-06-08T07:51:39","guid":{"rendered":"https:\/\/eiposgrados.com\/?p=35478"},"modified":"2021-06-08T10:10:15","modified_gmt":"2021-06-08T08:10:15","slug":"create-a-calculator-in-python","status":"publish","type":"post","link":"https:\/\/eiposgrados.com\/eng\/python-blog\/create-a-calculator-in-python\/","title":{"rendered":"Python 2.0 Calculator"},"content":{"rendered":"<p>A few weeks ago we created our <a href=\"https:\/\/eiposgrados.com\/eng\/python-blog\/how-to-make-python-calculator\/\">first calculator in python<\/a> and in the previous post we learned to <a href=\"https:\/\/eiposgrados.com\/eng\/python-blog\/how-to-make-a-function-in-python\/\">create our own functions<\/a>.<\/p>\n\n\n\n<p>Well, this week we are going to improve our calculator by creating functions for each of the arithmetic operations.<\/p>\n\n\n\n<p>I&#039;m sure you&#039;ll have as much fun as we do improving our calculator on<strong> Python<\/strong> and remember that if you want to continue learning you can do it with our <a href=\"https:\/\/eiposgrados.edu.es\/master-en-python\/\" target=\"_blank\" rel=\"noopener\">master in Advanced Programming in Python for Big Data, Hacking and Machine Learning.<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Code from which we start<\/h2>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img fetchpriority=\"high\" decoding=\"async\" src=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/Primera-calculadora-en-Python-869x1024.png\" alt=\" create a calculator in python\" class=\"wp-image-35479\" width=\"623\" height=\"733\" title=\"\" srcset=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/Primera-calculadora-en-Python-869x1024.png 869w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/Primera-calculadora-en-Python-254x300.png 254w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/Primera-calculadora-en-Python-768x905.png 768w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/Primera-calculadora-en-Python-140x165.png 140w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/Primera-calculadora-en-Python.png 1140w\" sizes=\"(max-width: 623px) 100vw, 623px\" \/><\/figure><\/div>\n\n\n\n<p>We start from this code, when we execute it we enter a loop <strong><em>while<\/em><\/strong> (if you don&#039;t know what it is, I&#039;ll explain it to you <a href=\"https:\/\/eiposgrados.com\/eng\/python-blog\/how-to-make-a-while-loop-in-python\/\">here<\/a>) where the calculator menu is shown. In this menu we have several options to perform arithmetic operations.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Create functions<\/h2>\n\n\n\n<p>Well, we are going to create functions for each of the cases and thus, in this way, have a <strong>much neater code.<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img decoding=\"async\" src=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/Calculadora-en-python-1024x827.png\" alt=\"\" class=\"wp-image-35480\" width=\"591\" height=\"477\" title=\"\" srcset=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/Calculadora-en-python-1024x827.png 1024w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/Calculadora-en-python-300x242.png 300w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/Calculadora-en-python-768x620.png 768w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/Calculadora-en-python-204x165.png 204w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/Calculadora-en-python.png 1162w\" sizes=\"(max-width: 591px) 100vw, 591px\" \/><\/figure><\/div>\n\n\n\n<p>We start by declaring the variables as global <em>number 1<\/em> and <em>number2<\/em> to be able to use them in any function and we create the functions to <strong>add, subtract, multiply and divide <\/strong>They will receive two parameters that will be number 1 and 2.&nbsp;<\/p>\n\n\n\n<p>In the <strong>division function<\/strong> We create an if to validate that the value of \u201cb\u201d is not zero. If it is, we throw the error message because it cannot be divided by zero.<\/p>\n\n\n\n<p>We have omitted the <strong>change of numbers<\/strong> since on this occasion we will request the numbers every time we want to do an arithmetic operation.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img decoding=\"async\" src=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/como-hacer-una-calculadora-en-python-641x1024.png\" alt=\"\" class=\"wp-image-35481\" width=\"403\" height=\"644\" title=\"\" srcset=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/como-hacer-una-calculadora-en-python-641x1024.png 641w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/como-hacer-una-calculadora-en-python-188x300.png 188w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/como-hacer-una-calculadora-en-python-768x1227.png 768w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/como-hacer-una-calculadora-en-python-103x165.png 103w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/como-hacer-una-calculadora-en-python.png 904w\" sizes=\"(max-width: 403px) 100vw, 403px\" \/><\/figure><\/div>\n\n\n\n<p>In the next section of code, we change to <strong>true<\/strong> the value to validate the <em>while<\/em>, and we put each function in its place, calling the function first <strong><em>enterNumbers()<\/em><\/strong> and then to the function that performs the operation we have chosen.<\/p>\n\n\n\n<p>Finally we have added<strong> <em>break<\/em><\/strong> to get out of the loop <em>while<\/em> when the user decides. We leave you some screenshots with the results.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/hacer-una-calculadora-en-python-1024x740.png\" alt=\"\" class=\"wp-image-35482\" width=\"652\" height=\"471\" title=\"\" srcset=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/hacer-una-calculadora-en-python-1024x740.png 1024w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/hacer-una-calculadora-en-python-300x217.png 300w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/hacer-una-calculadora-en-python-768x555.png 768w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/hacer-una-calculadora-en-python-228x165.png 228w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/hacer-una-calculadora-en-python.png 1398w\" sizes=\"(max-width: 652px) 100vw, 652px\" \/><\/figure><\/div>","protected":false},"excerpt":{"rendered":"<p>At EIP we continue learning how to create a calculator in Python. In this case, we see how to do them with functions for each operation. Forward!<\/p>","protected":false},"author":51,"featured_media":35499,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[407],"tags":[],"class_list":["post-35478","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog-python"],"acf":[],"_links":{"self":[{"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/posts\/35478","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/users\/51"}],"replies":[{"embeddable":true,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/comments?post=35478"}],"version-history":[{"count":0,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/posts\/35478\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/media\/35499"}],"wp:attachment":[{"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/media?parent=35478"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/categories?post=35478"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/tags?post=35478"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}