0

As you know input returns a string.

listOfNums = input("Type Numbers ")

def calcR(num1, num2):
    return 1 / ((1 / num1) + (1 / num2))

but here in reduce I need a list of numbers to place it in the function.

result = round(reduce(calcR, list(listOfNums)), 5)

If I run the app like that and type numbers in the input prompt I will have an error that tells me that you entered a string value and the function needs an integer value

  • Do you know how to split a string into a list of strings using a space delimiter? Do you know how to convert a string to an integer? Put those two things together. – Barmar Sep 11 '21 at 21:57

1 Answers1

0

I believe you are looking for map:

from functools import reduce


listOfNums = input("Type Numbers ")
m = list(map(int, listOfNums))

def calcR(num1, num2):
    return 1 / ((1 / num1) + (1 / num2))

result = round(reduce(calcR, m), 5)
rv.kvetch
  • 5,465
  • 3
  • 10
  • 28
  • but there is another problem happens if I wanna enter a number consists of 2 nums like 18 it identify it as [1, 8] – Sayed Reda Sep 12 '21 at 09:06
  • you can split the input based on a space character “ “ and get a new list. You can then pass the list to the `map` function, it should work the same to convert all elements to a number type. – rv.kvetch Sep 12 '21 at 14:10
  • I'm sorry, can you pls code that in a comment because I tried it and it didn't work for me – Sayed Reda Sep 12 '21 at 19:38