Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Cant get desired output!
#1
I need three different outputs for this function.. I cant figure out why it gives me the same answer no matter what I enter!

>>> def DSValue(bina, binb, binc):
               if bina < 16 and binb < 13 and binc < 10:
                         dsv=0
                         print "bina DSV = '%s', binb DSV = '%s', binc DSV = '%s'" % (dsv, dsv, dsv)
               elif bina < 19 and binb < 16 and binc < 13:
                         dsv=1
                         print "bina DSV = '%s', binb DSV = '%s', binc DSV = '%s'" % (dsv, dsv, dsv)
               elif bina < 22 and binb < 19 and binc < 16:
                          dsv=2
                          print "bina DSV = '%s', binb DSV = '%s', binc DSV = '%s'" % (dsv, dsv, dsv)
               elif bina < 25 and binb < 22 and binc < 19:
                           dsv=3
                           print "bina DSV = '%s', binb DSV = '%s', binc DSV = '%s'" % (dsv, dsv, dsv)
               elif bina <26 and binb < 25 and binc < 22:
                            dsv=4
                            print "bina DSV = '%s', binb DSV = '%s', binc DSV = '%s'" % (dsv, dsv, dsv)
               else:
                            print "WRONGGGG!!!"


>>> DSValue(15, 15, 15)
bina DSV = '2', binb DSV = '2', binc DSV = '2'
>>> # The answer should be
>>> # bina DSV = '0', binb DSV = '1', binc DSV = '2'


If anyone has any suggestions please let me know!!!
Reply
#2
When your code is:

    dsv=2
    print "bina DSV = '%s', binb DSV = '%s', binc DSV = '%s'" % (dsv, dsv, dsv)
... you insert three times the same value 2 in bina DSV = '%s', binb DSV = '%s', binc DSV = '%s
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
Is their a way to change it so that it can have an output that would be the "dsv" of all the variables depending on the numbers inserted into the function?
Reply
#4
If you need a separate "dsv" value for each of the input variables (bina, binb, binc), then you need three variables (say: dsva, dsvb, dsvc).

And since each of these variables will have independent values, you need 3 groups of if/elif/else, one for each input variable.

If you want to print a "wrong input" message, then initialize each of the dsv variables to something recognizable such as None,
and at the end of your code, test if any is still None, if so print the error message else print the three dsv values.
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
#5
Thank you!!!!
Reply
#6
Also, you could handle the the problem differently. Assume you have variables:
thresholdsA=[19,22,25,26]
thresholdsB=[16,19,22,25]
thresholdsC=[13,16,19,22]
Then you can scan the array until you hit something bigger than your input, and the index where you stop is your dsv value.

And then you notice that you are doing the same thing for the three inputs, just using a different list of thresholds, then instead of duplicating the code you can use a function that takes the input value and the lists, and return the dsv (or None).
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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Json filter is not capturing desired key/element mrapple2020 1 1,120 Nov-24-2022, 09:22 AM
Last Post: ibreeden
  how can I display only desired items? 3lnyn0 5 2,021 Dec-25-2021, 06:49 PM
Last Post: menator01
  ERROR: importing desired module mbgamer28 0 1,673 Apr-05-2021, 07:46 PM
Last Post: mbgamer28
  Not rounding to desired decimal places? pprod 2 2,542 Mar-05-2021, 11:11 AM
Last Post: pprod
  Why this code not getting desired output ? MDRI 2 2,522 Sep-18-2020, 02:11 AM
Last Post: MDRI
  showing only desired part of a plot grknkilicaslan 1 2,329 Jul-10-2020, 03:51 PM
Last Post: Marbelous
  [Beginner] Code is not producing desired result fakej171 2 2,422 Mar-21-2020, 10:26 AM
Last Post: buran
  Not Getting the Desired value using json.dumps saurabh210 0 1,499 Feb-03-2020, 06:48 PM
Last Post: saurabh210
  Visual Studio Code does not print desired output but only prints "..." vincentolivers 11 9,699 Sep-09-2019, 12:58 PM
Last Post: vincentolivers
  Recursion through nested dictionary - Unable to get desired output Staedtler 6 10,057 Jul-09-2018, 02:55 PM
Last Post: Staedtler

Forum Jump:

User Panel Messages

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