user10620036
user10620036

Reputation:

How to allow user to enter no number for an operation in a calculator

This is my third project in Python. I'm working on a calculator, and I have all the operations etc., working just fine. However, I would like to allow the user to not have to input a second number on two of the operations ( that being Euler's number and Pi) and for the calculator to still work. For instance, if I would like to multiply a number by pi, right now, I would need to input '1' as the second number. I want to eliminate this need. I really appreciate any help you can provide.

import math


def welcome():
    print('''
Welcome to Calculator
''')


# Define our function
welcome()


def calculate():
    operation = input('''
Please type in the math operation you would like to complete:
+ for addition
- for subtraction
* for multiplication
/ for division
** for power
% for modulo
pi to multiply by Pi 
e to multiply by Euler's Number 
''')

    number_1 = float(input('Please enter the first number: '))
    number_2 = float(input('Please enter the second number: '))
    if operation == 'e':
        print('{} * e = '.format(number_1, math.e))
        print(number_1 * math.e)
    elif operation == 'pi':
        print('{} * Pi = '.format(number_1, math.pi))
        print(number_1 * math.pi)
    elif operation == '+':
        print('{} + {} = '.format(number_1, number_2))
        print(number_1 + number_2)
    elif operation == '-':
        print('{} - {} = '.format(number_1, number_2))
        print(number_1 - number_2)
    elif operation == '*':
        print('{} * {} = '.format(number_1, number_2))
        print(number_1 * number_2)
    elif operation == '/':
        print('{} / {} = '.format(number_1, number_2))
        print(number_1 / number_2)
    elif operation == '**':
        print('{} ** {} ='.format(number_1, number_2))
        print(number_1 ** number_2)
    elif operation == '%':
        print('{} % {} ='.format(number_1, number_2))
        print(number_1 % number_2)
    else:
        print('You have not typed a invalid operator, please run the program again.')

    # Add again() function to calculate() function
    again()


def again():
    calc_again = input('''
Do you want to calculate again?
Please type Y for YES or N for NO.
''')

    if calc_again.upper() == 'Y':
        calculate()
    elif calc_again.upper() == 'N':
        print("Thank you for using the calculator. I'll be here whenever you need me.")
    else:
        again()


# Call calculate() outside of the function
calculate()

Upvotes: 1

Views: 197

Answers (3)

TomM
TomM

Reputation: 175

You could solve this problem without too many changes to your code by moving the input for the second number into all the loops after your conditional checks for e and pi.

As an example:

    number_1 = float(input('Please enter the first number: '))
    if operation == 'e':
        print('{} * e = '.format(number_1, math.e))
        print(number_1 * math.e)
    elif operation == 'pi':
        print('{} * Pi = '.format(number_1, math.pi))
        print(number_1 * math.pi)
    elif operation == '+':
        number_2 = float(input('Please enter the second number: '))
        print('{} + {} = '.format(number_1, number_2))
        print(number_1 + number_2)

This doesn't follow best practices, but it should get done what you want. If you want a better way to do it than this, I can edit this answer to include the improvements and why they are better.

Upvotes: 0

rhurwitz
rhurwitz

Reputation: 2737

Zeroing in on the specific fragment you would need to change, here is one way to do it:

...
    number_1 = float(input('Please enter a number: '))
    if operation == 'e' or operation == 'pi':
        if operation == 'e':
            print('{} * e = '.format(number_1, math.e))
            print(number_1 * math.e)
        elif operation == 'pi':
            print('{} * Pi = '.format(number_1, math.pi))
            print(number_1 * math.pi)
    else:
        number_2 = float(input('Please enter a second number: '))
        if operation == '+':
            print('{} + {} = '.format(number_1, number_2))
            print(number_1 + number_2)
        elif operation == '-':
            print('{} - {} = '.format(number_1, number_2))
            print(number_1 - number_2)
...

Upvotes: 1

lyncx
lyncx

Reputation: 680

Just add an if condition above

number_2 = float(input('Please enter the second number: '))

Like this

if(operation != 'e' || operation != 'pi'):
   number_2 = float(input('Please enter the second number: '))

Upvotes: 1

Related Questions