Mathematical Expression in Python

preview_player
Показать описание
Mathematics

Find which math expression matches the answer that you are given, if you have an integer answer, and a list of math expressions.

Task:
Test each math expression to find the first one that matches the answer that you are given.

Input Format:
Two inputs: an integer and a space separated string of math expressions. The following operations need to be supported: addition +, subtraction -, multiplication *, division /.
An expression can include multiple operations.

Output Format:
A string that tells the index of the first math expression that matches. If there are no matches, output 'none'.

Sample Input:
15
(2+100) (5*3) (14+1)

Sample Output:
index 1

Explanation:
Index counting starts at 0, so '(5*3)' is at index 1 and matches your answer of 15.

Code:

num = int(input())
exp = str(input())
index = 0
found = False

i = i[1:-1]
if num == eval(i):
found = True
break
index += 1
if found:
print("index",index)
else:
print("none")

#Python3 #SoloLearn #Challenge #coding #Expression #mathematics #Programmer #programmingwithpython #kafeel
Рекомендации по теме