DEV Community

Cover image for Day 09 of My AI & Data Mastery Journey: From Python to Generative AI
Nitin-bhatt46
Nitin-bhatt46

Posted on

Day 09 of My AI & Data Mastery Journey: From Python to Generative AI

_Project :- _

Calculator Program

Define a function to get the first number from the user

Prompt user to enter a number, convert to float, and return it

Define a function to get the second number from the user

Prompt user to enter another number, convert to float, and return it

Define a function to choose the operation:

Create a dictionary mapping operation symbols ("+", "-", "*", "/") to their functions

For each operation symbol, display it to the user

Ask user to select an operation

Call the corresponding function with the first number and a new number input

Return the computed result

Define functions for addition, subtraction, multiplication, and division

Each function takes two numbers and returns the result of the operation

Define a function to determine user's next command:

If the user chooses "y", get another number, use the previous result as the first input, and repeat the operation

Display the new result and offer future choices

If the user chooses "n", restart with a new calculation

If the user chooses anything else, exit the program

Define a function to ask the user if they want to continue calculating with the current answer, start a new calculation, or exit

Define main function:

Call operator on first number input

Display the calculated result

Offer the user future calculation options

Start the program by calling main

Thank you for today see this code in my github repo.

Top comments (0)