<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: rezanezhadfatemeh0-cmd</title>
    <description>The latest articles on Forem by rezanezhadfatemeh0-cmd (@rezanezhadfatemeh0cmd).</description>
    <link>https://forem.com/rezanezhadfatemeh0cmd</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3547372%2Fb3ba9402-2738-41ed-b266-56901a38a93d.png</url>
      <title>Forem: rezanezhadfatemeh0-cmd</title>
      <link>https://forem.com/rezanezhadfatemeh0cmd</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/rezanezhadfatemeh0cmd"/>
    <language>en</language>
    <item>
      <title>Python Calculator</title>
      <dc:creator>rezanezhadfatemeh0-cmd</dc:creator>
      <pubDate>Sun, 05 Oct 2025 21:50:54 +0000</pubDate>
      <link>https://forem.com/rezanezhadfatemeh0cmd/python-calculator-26b</link>
      <guid>https://forem.com/rezanezhadfatemeh0cmd/python-calculator-26b</guid>
      <description>&lt;h1&gt;
  
  
  Simple Python Calculator
&lt;/h1&gt;

&lt;p&gt;A clean command-line calculator built with Python. Performs basic arithmetic operations with error handling.&lt;/p&gt;

&lt;h2&gt;
  
  
  What It Does
&lt;/h2&gt;

&lt;p&gt;This calculator lets you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add, subtract, multiply, and divide numbers&lt;/li&gt;
&lt;li&gt;Handle decimals and negative numbers&lt;/li&gt;
&lt;li&gt;Prevent crashes from bad input&lt;/li&gt;
&lt;li&gt;Run multiple calculations without restarting&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How It Works
&lt;/h2&gt;

&lt;h3&gt;
  
  
  The Functions
&lt;/h3&gt;

&lt;p&gt;Each math operation gets its own function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;divide&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Error! Division by zero.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why separate functions?&lt;/strong&gt; Makes the code easy to test and reuse.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Menu Loop
&lt;/h3&gt;

&lt;p&gt;The calculator runs in a loop, showing a menu each time:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1. Add
2. Subtract
3. Multiply
4. Divide
5. Exit
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Users pick a number, enter two values, and get their answer. Press 5 to exit.&lt;/p&gt;

&lt;h3&gt;
  
  
  Error Handling
&lt;/h3&gt;

&lt;p&gt;Two types of errors are handled:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Division by zero&lt;/strong&gt; - Returns an error message instead of crashing&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Invalid input&lt;/strong&gt; - Catches when users type letters instead of numbers
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;num1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;float&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Enter first number: &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Invalid input! Please enter numbers only.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Key Features
&lt;/h2&gt;

&lt;p&gt;✓ Simple menu interface&lt;br&gt;&lt;br&gt;
✓ Works with decimals (3.14, 2.5)&lt;br&gt;&lt;br&gt;
✓ Works with negatives (-5, -10)&lt;br&gt;&lt;br&gt;
✓ Doesn't crash on bad input&lt;br&gt;&lt;br&gt;
✓ Only 50 lines of code  &lt;/p&gt;

&lt;h2&gt;
  
  
  Usage Example
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Enter choice (1/2/3/4/5): 1
Enter first number: 10
Enter second number: 5
10.0 + 5.0 = 15.0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What I Learned
&lt;/h2&gt;

&lt;p&gt;Building this taught me:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Writing clean functions&lt;/li&gt;
&lt;li&gt;Handling user input safely&lt;/li&gt;
&lt;li&gt;Using loops for repeated actions&lt;/li&gt;
&lt;li&gt;Managing errors without crashes&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Possible Improvements
&lt;/h2&gt;

&lt;p&gt;Ideas for future versions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add more operations (square root, power)&lt;/li&gt;
&lt;li&gt;Save calculation history&lt;/li&gt;
&lt;li&gt;Add a memory function&lt;/li&gt;
&lt;li&gt;Create a GUI version&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Running the Code
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python calculator.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Tech&lt;/strong&gt;: Python 3&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Lines of Code&lt;/strong&gt;: ~50  &lt;/p&gt;

&lt;p&gt;View the complete code on GitHub: &lt;a href="https://github.com/rezanezhadfatemeh0-cmd/Simple-Calculator" rel="noopener noreferrer"&gt;Simple-Calculator&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Simple, functional, and ready to use.&lt;/p&gt;

</description>
      <category>cli</category>
      <category>python</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
