0

why no output

def strongBrain ():     a=input('integer a:\n')     b=input('integer b:\n')     c=input('integer c:\n')     d=input('integer d:\n')     str=('+','-','*','/')     flag=1     for param_1 in str:         for param_2 in str:             for param_3 in str:                 formula=a+param_1+b+param_2+c+param_3+d                 value=eval(formula)                 if value==24:                     flag=0                     print(formula)     if flag:         print('NONE')

25th Nov 2016, 3:30 AM
boredbird
2 Answers
+ 1
The only reason there could be no output is that you defined your function, but forgot to call it: if __name__ == '__main__': strongBrain()
25th Nov 2016, 9:52 AM
donkeyhot
donkeyhot - avatar
+ 1
Also, let me give you some advice on your code. 1. str is a built in python method and you've reassigned it as your own variable. So, next time you call str() method, it will lead to an error. You should've chosen a different name, for instance, ops or operands. 2. I understand, that you're only starting using python, but still. Your code is too 'c-style'. If you are learning python, try making use of its possibilities and write 'pythonic' style. For instance: a=input('integer a:\n') b=input('integer b:\n') #you've typed the same instruction 4 times c=input('integer c:\n') #which is ugly d=input('integer d:\n') a,b,c,d = (input('integer {}:\n'.format(i)) for i in 'abcd') #much prettier Using flags and too deeply nested loops is also a bad idea. Python allows you to avoid this: flag=1 #you don't need flags in python for ...: for...: #this is too nested for...: if...: So, I have rewritten your code in a python manner and got smth like that: from itertools import combinations_with_replacement as comb def pythonic_brain(): a,b,c,d = (input('integer {}:\n'.format(i)) for i in 'abcd') ops = '+-/*' f = (a+op1+b+op2+c+op3+d for (op1,op2,op3) in comb(ops,3)) f24 = filter(lambda x: eval(x)==24, f) if f24: for i in f24: print(i) else: print('None') if __name__ == '__main__': pythonic_brain() https://code.sololearn.com/c713OqcJ9arV/#py
25th Nov 2016, 6:09 PM
donkeyhot
donkeyhot - avatar