+ 1

Multiple string values

Input: 2 2 (two numbers with space) Output: 4 how to make it work without imort additional modules? (The entered numbers can be multi-digit (123 12 75533, etc.).)

30th Mar 2021, 7:21 AM
Sergey Rogalenko
12 Answers
+ 5
there is a builtin function to multiply elements of a list, but he is asking to use no imports import math print(math.prod([2,2,3,])) # result is 12
30th Mar 2021, 11:18 AM
Lothar
Lothar - avatar
+ 5
Sergey , your code is perfect, no need to rework it. 👍
30th Mar 2021, 11:20 AM
Lothar
Lothar - avatar
+ 2
Илья Мирошник I'd advise against using eval on input without any validation because it poses a security risk. This just a training exercise of course, but suggesting it shouldn't go without a warning about the risks. At least don't use it on untrusted input in a productive application. More explanation: https://realpython.com/JUMP_LINK__&&__python__&&__JUMP_LINK-eval-function/#minimizing-the-security-issues-of-eval Also you are making assumptions about input that can be false And it is harder to read / understand the purpose than math.prod()
30th Mar 2021, 5:23 PM
Benjamin Jürgens
Benjamin Jürgens - avatar
+ 1
Not sum(), need multiple(*)....
30th Mar 2021, 7:30 AM
Sergey Rogalenko
+ 1
Works, but not very elegantly 😂 https://code.sololearn.com/c8M7QShM3ZRS/?ref=app
30th Mar 2021, 9:14 AM
Sergey Rogalenko
+ 1
Lothar you are right, my wording was not correct
30th Mar 2021, 11:24 AM
Benjamin Jürgens
Benjamin Jürgens - avatar
+ 1
Just eval text: print(eval(str(input()).replace(" ","*")))
30th Mar 2021, 1:18 PM
Илья Мирошник
Илья Мирошник - avatar
0
There is no inbuilt function to multiply list elements, so you have to do it yourself in a loop. If you can't get it to work have a look at this link: https://www.geeksforgeeks.org/JUMP_LINK__&&__python__&&__JUMP_LINK-multiply-numbers-list-3-different-ways/
30th Mar 2021, 8:43 AM
Benjamin Jürgens
Benjamin Jürgens - avatar
0
Илья Мирошник Лучшее решение, спасибо! print(eval(str(input()).replace(" ","*")))
30th Mar 2021, 1:22 PM
Sergey Rogalenko
0
eval can be made a programming language itself lol
30th Mar 2021, 5:24 PM
Carlos
Carlos - avatar
0
LOL its just fun man
30th Mar 2021, 5:25 PM
Илья Мирошник
Илья Мирошник - avatar
0
Carlos right, although the language is still python of course. Or to put it different you can say that this: eval(input()) is a truly universal program that can do literally anything.
30th Mar 2021, 5:31 PM
Benjamin Jürgens
Benjamin Jürgens - avatar