Python Forum
Python if else condition using with args - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Python if else condition using with args (/thread-3703.html)



Python if else condition using with args - emrebt39 - Jun-14-2017

I want to use if else condition with arguments.Main codes at part 1
And i want to divide each arg to 10 and put args in if else condition.If else try part 2
How can i write if else condition using with args?
Part1
   def acc_callback(self, path, args):
       acc_x, acc_y, acc_z = args
       print "%s %f %f %f" % (path, acc_x, acc_y, acc_z)
Part2
   If(acc_x/10>40): 
   Print " bigger"
   Else:
   Print "smaller"



RE: Python if else condition using with args - Ofnuts - Jun-15-2017

There is no "else with arguments" (never met any language with such a concept). What you have is if/elif/else if you want to perform several tests:

if condition1:
   ## code executed if condition1 is true
elif condition2:
   ## code executed if condition1 is false and condition2 is true
elif condition3:
   ## code executed if condition1 and condition2 are false and condition3 is true
else:
   ## code executed when all conditions above are false



RE: Python if else condition using with args - buran - Jun-15-2017

first of all, the way you have args in your code, it will expect single object - list, tuple, etc., i.e. some data structure.

If I understand you correctly, you want to loop over all elements in args:

# python3 code
for arg in args:
    if arg/10 > 40:
        print('bigger')
    else:
        print('smaller')



RE: Python if else condition using with args - emrebt39 - Jun-15-2017

(Jun-15-2017, 10:19 AM)buran Wrote: first of all, the way you have args in your code, it will expect single object - list, tuple, etc., i.e. some data structure.

If I understand you correctly, you want to loop over all elements in args:

# python3 code
for arg in args:
    if arg/10 > 40:
        print('bigger')
    else:
        print('smaller')

Thanks.You divide all args to 10.If i use each arg in different process,what can ido?e.g my args are :x y and z.i will divide x to 10 but divide y to 20...


RE: Python if else condition using with args - buran - Jun-15-2017

(Jun-15-2017, 06:42 PM)emrebt39 Wrote: You divide all args to 10.If i use each arg in different process,what can ido?e.g my args are :x y and z.i will divide x to 10 but divide y to 20...

but in the OP

(Jun-14-2017, 08:58 PM)emrebt39 Wrote: And i want to divide each arg to 10 and put args in if else condition.If else try part 2

maybe you should decide what you want first :-)

this is very broad - it depends what your overall goal is. most simple - make separate if for each arg, but I would do something like this


args = [5000, 200, 70000]
divisors = [10, 20, 25]
benchmarks = [40, 20, 60]

for arg, divisor, benchmark in zip(args, divisors, benchmarks):
    print('{} / {} = {} :: benchmark {}'.format(arg, divisor, arg/divisor, benchmark))
    if arg/divisor > benchmark:
        print('bigger')
    else:
        print('smaller')
Output:
5000 / 10 = 500.0 :: benchmark 40 bigger 200 / 20 = 10.0 :: benchmark 20 smaller 70000 / 25 = 2800.0 :: benchmark 60 bigger



RE: Python if else condition using with args - emrebt39 - Jun-15-2017

(Jun-15-2017, 07:43 PM)buran Wrote:
(Jun-15-2017, 06:42 PM)emrebt39 Wrote: You divide all args to 10.If i use each arg in different process,what can ido?e.g my args are :x y and z.i will divide x to 10 but divide y to 20...
but in the OP
(Jun-14-2017, 08:58 PM)emrebt39 Wrote: And i want to divide each arg to 10 and put args in if else condition.If else try part 2
maybe you should decide what you want first :-) this is very broad - it depends what your overall goal is. most simple - make separate if for each arg, but I would do something like this
args = [5000, 200, 70000] divisors = [10, 20, 25] benchmarks = [40, 20, 60] for arg, divisor, benchmark in zip(args, divisors, benchmarks): print('{} / {} = {} :: benchmark {}'.format(arg, divisor, arg/divisor, benchmark)) if arg/divisor > benchmark: print('bigger') else: print('smaller')
Output:
5000 / 10 = 500.0 :: benchmark 40 bigger 200 / 20 = 10.0 :: benchmark 20 smaller 70000 / 25 = 2800.0 :: benchmark 60 bigger

Thank you Buran.You are guru :)