Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

[open] Text input function - add maximum & minimum

edited June 2015 in OpenSesame

Hello,

I'd like to clamp the text input value, so that if participants go above a maximum value or go below a minimum value the text_input prints the the max or min values.

I added in this code in a separate inline script, but it still logs the original values:

# add max and min values
min_num = 0.50
max_num = 100

def clamp(text_input, min_num, max_num):
     if text_input < min_num:
        return min_num
     elif text_input > max_num:
        return max_num
     else:
        return text_input

text_input = clamp(text_input, min_num, max_num)


I also tried it in the same inline script as the text input function:

#import widgets
from libopensesame import widgets

# set columns and rows
form = widgets.form(self.experiment,cols=1, rows=3)

# add instruction
label = widgets.label(form, text='How much are you willing to offer in steps of 50 Cents?<br /><br />Click inside the box, type the amount and press ENTER.')

# add image
image = widgets.image(form, path=self.experiment.get_file(self.get('video_image')))

# add text input box
text_input = widgets.text_input(form, var='response', return_accepts=True)

# add max and min values
min_num = 0.50
max_num = 100

def clamp(text_input, min_num, max_num):
     if text_input < min_num:
        return min_num
     elif text_input > max_num:
        return max_num
     else:
        return text_input

text_input = clamp(text_input, min_num, max_num)

# set order of widgets 
form.set_widget(label, (0,0))
form.set_widget(image, (0,1))
form.set_widget(text_input, (0,2))
form._exec()

But it kept coming up with the error:

Error while executing inline script
phase: run
item: Responder
line: 314
exception message: 'int' object has no attribute 'set_rect'
exception type: AttributeError

Traceback (also in debug window):
  File "dist\libopensesame\inline_script.py", line 173, in run
  File "dist\libopensesame\python_workspace.py", line 111, in _exec
  File "<string>", line 34, in <module>
  File "dist\libopensesame\widgets\_form.py", line 315, in set_widget
AttributeError: 'int' object has no attribute 'set_rect'

Thanks in advance :)

Comments

  • edited 9:42AM

    Hi Hollana,

    My apologies, but it's not really clear to me what you want to do. The code you copied here,

        def clamp(text_input, min_num, max_num): 
             if text_input < min_num: 
                   return min_num 
             elif text_input > max_num: 
                   return max_num 
             else: return text_input
    
             text_input = clamp(text_input, min_num, max_num)
    

    What do you exactly hope for this code to do?
    I guess you want people to indicate an amount, and just give a kind of warning when this amount is not within the given boundaries? Something like that can be easily implemented; however I need to know what strategy you were aiming for before I can point you in the right direction.

    Cheers,

    Josh

  • edited 9:42AM

    Hi Josh,

    Thanks for the reply. Sorry for the confusion.

    I'd like people to indicate an amount, but if that amount falls below a minimum or maximum number then a warning should come up and they have to change the amount.

    Cheers!

  • edited June 2015

    Alright!
    Did you already manage to get this to work without the minimum/maximum message? What you could do is place the form item in a loop. Clicking on the loop item, you can tick an 'advanced options' box to reveal a break-if statement. Here you can make sure that your form is repeated until participants give a response that is within your desired boundaries - for instance: break if: =self.get('done')==True.
    Now inside the loop item, you have to place a second form, which is a copy of the first one but with the addition of your warning message. (By the way, the loop item only takes one item; hence you need to place a sequence item in the loop item, and add both forms to the sequence). The form with a warning message should only be played if the response was below the minimum or above the maximum. Clicking on the sequence item, you can define a run-if statement for this form: run if: [response]maximum. Further, the first form (without warnings) should only be run the first time, so here you can insert: [first_time] = True
    What you then need to do is alter the variable 'done' based on what people responded. So you'd place an inline_script at the end of your sequence, and insert the commands:

         first_time = False
         if minimum <= self.get('response') <= maximum:
                 done = True 
    

    Lastly, the experiment will crash if it runs all of the above run-if statements while there is no such existing variable. Frankly, this is the case with the first iteration through the loop, since both 'done' and 'first_time' are made in the inline_script. Thus, you'd only need to place an additional inline script somewhere before the loop, and create these variables:

          first_time = True
          response = 0 
          exp.set('response', response) #makes the variable available outside inline_scripts
          exp.set('first_time', first_time) # so you can call it in a loop-item for instance.   
    

    Let me know how it goes.

    Cheers,

    Josh

Sign In or Register to comment.

agen judi bola , sportbook, casino, togel, number game, singapore, tangkas, basket, slot, poker, dominoqq, agen bola. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 50.000 ,- bonus cashback hingga 10% , diskon togel hingga 66% bisa bermain di android dan IOS kapanpun dan dimana pun. poker , bandarq , aduq, domino qq , dominobet. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 10.000 ,- bonus turnover 0.5% dan bonus referral 20%. Bonus - bonus yang dihadirkan bisa terbilang cukup tinggi dan memuaskan, anda hanya perlu memasang pada situs yang memberikan bursa pasaran terbaik yaitu http://45.77.173.118/ Bola168. Situs penyedia segala jenis permainan poker online kini semakin banyak ditemukan di Internet, salah satunya TahunQQ merupakan situs Agen Judi Domino66 Dan BandarQ Terpercaya yang mampu memberikan banyak provit bagi bettornya. Permainan Yang Di Sediakan Dewi365 Juga sangat banyak Dan menarik dan Peluang untuk memenangkan Taruhan Judi online ini juga sangat mudah . Mainkan Segera Taruhan Sportbook anda bersama Agen Judi Bola Bersama Dewi365 Kemenangan Anda Berapa pun akan Terbayarkan. Tersedia 9 macam permainan seru yang bisa kamu mainkan hanya di dalam 1 ID saja. Permainan seru yang tersedia seperti Poker, Domino QQ Dan juga BandarQ Online. Semuanya tersedia lengkap hanya di ABGQQ. Situs ABGQQ sangat mudah dimenangkan, kamu juga akan mendapatkan mega bonus dan setiap pemain berhak mendapatkan cashback mingguan. ABGQQ juga telah diakui sebagai Bandar Domino Online yang menjamin sistem FAIR PLAY disetiap permainan yang bisa dimainkan dengan deposit minimal hanya Rp.25.000. DEWI365 adalah Bandar Judi Bola Terpercaya & resmi dan terpercaya di indonesia. Situs judi bola ini menyediakan fasilitas bagi anda untuk dapat bermain memainkan permainan judi bola. Didalam situs ini memiliki berbagai permainan taruhan bola terlengkap seperti Sbobet, yang membuat DEWI365 menjadi situs judi bola terbaik dan terpercaya di Indonesia. Tentunya sebagai situs yang bertugas sebagai Bandar Poker Online pastinya akan berusaha untuk menjaga semua informasi dan keamanan yang terdapat di POKERQQ13. Kotakqq adalah situs Judi Poker Online Terpercayayang menyediakan 9 jenis permainan sakong online, dominoqq, domino99, bandarq, bandar ceme, aduq, poker online, bandar poker, balak66, perang baccarat, dan capsa susun. Dengan minimal deposit withdraw 15.000 Anda sudah bisa memainkan semua permaina pkv games di situs kami. Jackpot besar,Win rate tinggi, Fair play, PKV Games