1

I'm trying to take the first number from a list and use it as my initial value. When I started doing subtraction, I realized that you actually don't start at 0 as you do with adding number, but you start with first number written in 'for'.
For example, when you add 0+4+5, then 4+5 is actually the same thing; when subtracting, it's different to do 0-4-5 and 4-5. I'm just starting with Python and programming in general, so I'm wondering if this is even possible.

My code:

print ("You chose subtraction.")
b = int(input("Enter how many numbers you want to substract."))
c = [int (input( "Number: ")) for _ in range (b)]
result = functools.reduce(operator.sub, c, INIT_VALUE)
print("Your answer is:", result)
Alan Kavanagh
  • 9,425
  • 7
  • 41
  • 65

4 Answers4

1

You can get the first number that was entered by accessing c[0] ( or values[0] in my example ), you also only need to subtract the values after this index so you can use c[1:] ( or values[1:] in my example )

import operator
import functools

print("You chose subtraction.")
total_numbers = int(input("Enter how many numbers you want to substract."))
values = [int(input("Number: ")) for _ in range(total_numbers)]
result = functools.reduce(operator.sub, values[1:], values[0])
print("Your answer is:", result)

>> You chose subtraction.
>> Enter how many numbers you want to substract.
> 5
>> Number: 
> 10
>> Number: 
> 1
>> Number: 
> 1
>> Number: 
> 1
>> Number: 
> 1
>> Your answer is: 6
Alan Kavanagh
  • 9,425
  • 7
  • 41
  • 65
0

There are a few ways you can do this:

You could get the first number separately:

 >>> INIT_VALUE = input('Number: ')
 >>> numbers = [int(input('Number: ')) for _ in range(b - 1)]
 >>> result = reduce(sub, numbers, INIT_VALUE)

Alternatively, you could use indexing:

 >>> INIT_VALUE = c[0]
 >>> rest = c[1:]
 >>> result = reduce(sub, rest, INIT_VALUE)

Or, if you wanted to use a loop instead of reduce:

 >>> result = int(input('Number: '))
 >>> for _ in range(b - 1):
 ...     result -= int(input('Number: '))
Edward Minnix
  • 2,889
  • 1
  • 13
  • 26
0

You can do reduce(sub, lst[1:], lst[0]), but you can also skip the inifializer altogether

reduce(sub, lst)

If you won't provide it, the first value will be taken

https://docs.python.org/3/library/functools.html#functools.reduce

pacholik
  • 8,607
  • 9
  • 43
  • 55
0

Another option is itertools.accumulate in Python 3. This allows you see intermediate calculations.

import operator as op
import itertools as it


print("You chose subtraction.")
total_numbers = int(input("Enter how many numbers you want to substract? "))
values = [int(input("Number: ")) for _ in range(total_numbers)]

# You chose subtraction.
# Enter how many numbers you want to substract? 3
# Number: 0
# Number: 5
# Number: 6

results = list(it.accumulate(values, op.sub))
results
# [0, -5, -11]

results[-1]
# -11

See also this post comparing accumulate and reduce.

pylang
  • 40,867
  • 14
  • 129
  • 121
  • Oh yes, that's good to know for later, I'll definitely check it out later, but for now I will stick with basics :D – Niickeriino Sep 20 '17 at 16:30