0

So to start coding I decided to create a simple calculator which takes the input from the user, turns it into two separate lists, then asks which operations you want to use, and does the equation. The problem is though is that I will say I want to use subtraction, for example, my two numbers are 10 and 5, then the output is 15. Here is my code:

    first_numbers = [] 
second_number = []
maxLengthList = 1
while len(first_numbers) < maxLengthList:
    item = input("Enter Your First Number To Be Calculated: ")
    first_numbers.append(item)
while len(second_number) < maxLengthList:
    item = input("Enter Your Second Number To Be Calculated: ")
    second_number.append(item)


type_of_computing = raw_input('(A)dding  or  (S)ubtracting  or  (M)ultiplying  or  (Dividing): ')

if type_of_computing == 'A' or 'a':
    print ('Final Result:')
    sum_list = [a + b for a, b in zip(first_numbers, second_number)]
    print sum_list



elif type_of_computing == 'S' or 's':
    print ('Final Result:')
    difference_list = [c - d for c, d in zip(first_numbers, second_number)]
    print difference_list




elif type_of_computing == 'M' or 'm':
    print ('Final Result:')
    product_list = [e * f for e, f in zip(first_numbers, second_number)]
    print product_list




elif type_of_computing == 'D' or 'd':
    print ('Final Result:')
    quotient_list = [g / h for g, h in zip(first_numbers, second_number)]
    print quotient_list





 
martineau
  • 119,623
  • 25
  • 170
  • 301

2 Answers2

1

Your issue is the if statement.

if type_of_computing == 'A' or 'a':

should be if type_of_computing == 'A' or type_of_computing == 'a'

since you are checking for two different conditions. 'a' is a defined character, so it will always return true, which is why your code always adds. You should also fix the issue with the elif statements, and the problem should be resolved. However, I didn't test this on my own

Also, since you are trying to add two values, the inputs (each item for the first_numbers list and each item for second_number list) should be integers, not strings.

twiddler
  • 588
  • 4
  • 16
  • 1
    You may also do `if type_of_computing in ('A', 'a'):`. – felipe Mar 13 '21 at 19:34
  • Thank You, It Worked! but is there a way for me to also allow the lowercase version of the letters too? – Gabriel Burchfield Mar 13 '21 at 19:35
  • the or statement should allow for both uppercase and lowercase, regardless. However, if you want the variable type_of_computing to always be lowercase, use type_of_computing.lower(). If you want it to always be uppercase, use type_of_computing.upper(). – twiddler Mar 13 '21 at 19:39
0

The or expression in your check is always resulting in True because 'a' is a true (truthy) value. To test if a value is one of two things you can do:

if type_of_computing == 'A' or type_of_computing == 'a':

or:

if type_of_computing in ('A', 'a'):

but for the special case of case-insensitive comparisons, it's easier to just normalize the string with upper() or lower() and do a single comparison:

if type_of_computing.upper() == 'A'

You can also simplify this type of logic (and avoid a lot of copying and pasting) by using a dictionary instead of a bunch of if statements. Here's one way this code could be rewritten to be shorter:

maxLengthList = 1
first_numbers = [
    int(input("Enter Your First Number To Be Calculated: ")) 
    for _ in range(maxLengthList)
]
second_numbers = [
    int(input("Enter Your Second Number To Be Calculated: ")) 
    for _ in range(maxLengthList)
]


type_of_computing = input(
    '(A)dding  or  (S)ubtracting  or  (M)ultiplying  or  (Dividing): '
).upper()
compute = {
    'A': int.__add__,
    'S': int.__sub__,
    'M': int.__mul__,
    'D': int.__truediv__,
}[type_of_computing]

print('Final Result:')
print([compute(a, b) for a, b in zip(first_numbers, second_numbers)])
Samwise
  • 68,105
  • 3
  • 30
  • 44
  • Thank You, but I don't really understand the last part. Could you please explain? – Gabriel Burchfield Mar 13 '21 at 22:52
  • Instead of having a different `if` statement for each kind of computation you want to do, you can create a dictionary that maps the letter to the function -- e.g. `'A': int.__add__` maps the letter `'A'` to the builtin integer addition function, which is `int.__add__`. Use the input as the key in that dictionary, and you get the function. In the code above, I assigned that function to the name `compute`, and then just call `compute(a, b)` in the list comprehension at the end to get the final result. Try running the code so you can see how it works! :) – Samwise Mar 14 '21 at 00:43