Python Error "TypeError: 'str' object does not support item assignment"
Dear OS community,
I downloaded a template for my experiment and I tried to run it. Unfortunately it did not run, instead I got this error message immediately:
This error occurred on line 7 in the prepare phase of item Imports_and_Defaults.
Traceback (most recent call last): File "<Imports_and_Defaults.prepare>", line 7, in <module> TypeError: 'str' object does not support item assignment
The complete corresponding code of 'Imports_and_Defaults' is the following:
import random
var.balloon_sizes = {}
for i in range(0,20):
y_val = 300 - (15*i)
j = "%.2f" % ((20 - i) / 20.0)
var.balloon_sizes[j] = y_val
# Probability variable for random explosion time
var.Probability = 0.0
# Set up sound for successful inflation
src1 = exp.pool['casino960msMax_22k16bMono.wav']
Casino_sound = sampler(src1, volume=1)
# Set up sound for cashing out
src2 = exp.pool['finale_22k16bMono.wav']
Cash_out_sound = sampler(src2, volume=1)
total_pennies = 0.0
var.total_cash = "$%.2f" % total_pennies
prac_pennies = 0.0
var.prac_cash = "$%.2f" % prac_pennies
var.cash_out_button = "return"
var.inflate_button = "space"
var.response = 0
I could not manage to find out what the problem is. Would be great if anybody could help me with this. Thanks! 😊
Comments
Hi,
var.balloon_sizes = {}
I suppose you want to set up an empty dictionary? If so, you need to do
var.balloon = dict()
What you have done is setting up an empty set (which does not support assignments of strings (which is what j is)
Hope this clears things up.
Eduard