Python Forum
Python if else condition using with args
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python if else condition using with args
#1
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"
Reply
#2
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
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#3
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')
Reply
#4
(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...
Reply
#5
(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
Reply
#6
(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 :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Alteryx QS-Passing pandas dataframe column inside SQL query where condition sanky1990 0 689 Dec-04-2023, 09:48 PM
Last Post: sanky1990
Question How to compare two parameters in a function that has *args? Milan 4 1,183 Mar-26-2023, 07:43 PM
Last Post: Milan
  Python Pandas: How do I sumproduct by rows with an if condition? JaneTan 2 5,278 Jul-13-2021, 11:36 AM
Last Post: jefsummers
  *args implementation and clarification about tuple status amjass12 10 3,914 Jul-07-2021, 10:29 AM
Last Post: amjass12
  [SOLVED] Good way to handle input args? Winfried 2 2,003 May-18-2021, 07:33 PM
Last Post: Winfried
  Two Questions, *args and //= beginner721 8 3,422 Feb-01-2021, 09:11 AM
Last Post: buran
  else condition not called when if condition is false Sandz1286 10 5,737 Jun-05-2020, 05:01 PM
Last Post: ebolisa
  [HELP] Nested conditional? double condition followed by another condition. penahuse 25 7,700 Jun-01-2020, 06:00 PM
Last Post: penahuse
  Differential equations with initial condition in Python (change a working code) Euler2 1 1,793 May-29-2020, 04:06 PM
Last Post: Euler2
  does yield support variable args? Skaperen 0 1,641 Mar-03-2020, 02:44 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020