+ 1
Mathematics Code Coach Issues
I can't seem to find the answer to this problem. It fails for unknown test 3-5. Any input would be appreciated. https://code.sololearn.com/ccOP4EmLQZkh/?ref=app
11 Réponses
+ 2
try this :
answer = input()
formulas = input().split()
found = False
for i in formulas:
if eval(i) == int(answer):
print ("index" , formulas.index(i))
found = True
break
if not found:
print("none")
+ 1
answer = input()
formulas = input().split()
found = False
for i in formulas:
if eval(i) == int(answer):
out="index " + str(formulas.index(i) )
found = True
if not found:
out=("none")
print (out)
This one is completely correct code
0
I think that you should not remove parentheses.
0
Thanks for the advice. Tried it without removing the parentheses but still does not work.
0
does the codecoach ask you to print none? if so maybe print('none') should be out of the for loop.
0
It works for all the "none" test as it is. Here are the instructions for the challenge.
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
0
That totally worked! Can you explain to me what the difference between our code is?
0
Ohh I think I got it. My code would only work if the correct statement was in the 0 index otherwise I would print none. Thank you so much for the help.
0
yes, you got it. thank you too.
0
answ = int(input())
math_expr = input().split()
for i in math_expr :
if eval(i)==answ:
print("index " + str(math_expr.index(i)))
break
else :
print('none')
0
My solution:
score = int(input())
expressions = list(map(eval, input().split(" ")))
for x in expressions:
if score == x:
print("index", expressions.index(x))
break
else:
print("none")