0

I am still learning the fundamentals of Python, but I am getting stuck on this custom calculator program I am attempting to finish.

while True:

    print('Welcome. Below, type the number associated to begin calculating:')
    print('1: Addition')
    print('2: Subtraction')
    print('3: Multiplication')
    print('4: Division')

    pick = int(input('Enter the number associated with your calculation: '))
    if pick != (1, 5):
        print('Please enter an option given.')
    else:
        continue

def addNumber():
    if pick == 1:
        print('-------------------------')
        print('You have chosen #1 - Addition.')
        num1 = int(input('Enter your first number: '))
        num2 = int(input('Enter your second number: '))
        print('Your sum is equal to:', num1 + num2)
        print('-------------------------')

addNumber()

The first function is addition, and the rest of the functions are its own for each choice. This next part is the bottom, allowing the user to restart if chosen.

again = str(input('Would you like to calculate again?\n'))
if again == 'Yes':
    continue
else:
    break

There is no issue if the user tries to put a value outside of the range of 1-4 (prompting to try again), but if someone picks a shown option (like they're supposed to), 'Please enter an option given.' shows up.

Welcome. Below, type the number associated to begin calculating:
1: Addition
2: Subtraction
3: Multiplication
4: Division
Enter the number associated with your calculation: 1
Please enter an option given.
You have chosen #1 - Addition.
Enter your first number: 

How should I rewrite that if/else parameter?

Hiadore
  • 686
  • 3
  • 15
  • 1
    Possible duplicate of [How to test multiple variables against a value?](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value) – Makoto Feb 27 '19 at 23:55
  • @Makoto the testing of values is not the problem here. – ruohola Feb 28 '19 at 00:00

2 Answers2

0

Well, your if-block could look like this:

if pick not in range(1, 5):
    print('Please enter an option given.')
    continue
else:
    functions[pick]()

And you could define a functions dictonary on the top like this:

functions = {1: addNumber, 2: subNumber, 3: multiplyNumber, 4: divideNumber}

And make rest of the functions like you've already made the addNumber().

ruohola
  • 21,987
  • 6
  • 62
  • 97
0

I guess you have a problem in this line if pick in range(1, 5):

Here is my suggestion:

def addNumber():
    if pick == 1:
        print('-------------------------')
        print('You have chosen #1 - Addition.')
        num1 = int(input('Enter your first number: '))
        num2 = int(input('Enter your second number: '))
        print('Your sum is equal to:', num1 + num2)
        print('-------------------------')



while True:
    print('Welcome. Below, type the number associated to begin calculating:')
    print('1: Addition')
    print('2: Subtraction')
    print('3: Multiplication')
    print('4: Division')

    pick = int(input('Enter the number associated with your calculation: '))
    if pick in range(1, 5):
        if pick==1:
            addNumber()
        elif pick==2:
            subsNumber() # just as an example, but you can replace it 
        elif pick==3:
            MultipNumber() # same 
        else:
            divideNumber() # same
    else:
        print('Please enter an option given.')
smerllo
  • 3,117
  • 1
  • 22
  • 37
  • Is this more of a personal preference, or would you consider it more clean, good code? – AnacondianPython Feb 28 '19 at 05:02
  • There is no just one way to do it. This was my suggestion that I think it's better and cleaner. You can take and change as you want ! maybe you can use `pick not in range(1, 5)` and adjust it accordingly. – smerllo Mar 01 '19 at 08:35