How to fix error in code

How do you fix error in this code?

import bge
from bge import logic
cont = logic.getCurrentController()
 
own = cont.owner["prop"]
 
if own["prop"] == very:
    very = "something else"
 

Blender Game Engine Started
Python script error - object 'Cube', controller 'And':
Traceback (most recent call last):
  File "change.py", line 10, in <module>
TypeError: string indices must be integers



import bge
from bge import logic
cont = logic.getCurrentController()
 
own = cont.owner

very = "something" 

if own["prop"] == very:
    very = "something else"
 



You need to create the variable: “very”, before use it. Check the agoose77 message.

He also defines owner as cont.owner[‘prop’], which would seem incorrect.

The code seems to work but nothing happens.There are no errors in the console.

That’s because it doesn’t do anything… What do you expect it to do?

I wanted to change the prop variable to something else when i type in very.



from bge import logic
cont = logic.getCurrentController
own = cont.owner

if cont.sensors["Keyboard"].positive:
    own["prop"] = "something"

I already know how to do what is on post 8. I did not word it right.So i changed what was on post 7.

You can do something like this for entering text. it uses all keys with text logging.


from bge import logic
cont = logic.getCurrentController()
own = cont.owner

text = own["Text"]
length = len(text)

if own["Text"][length-1:length] == chr(10): #if last character is enter
    text = text[0:length-1] #strip last character (removes the line break character)
    if text == "very":
        ...do stuff...

The code seems to work but nothing happens.There are no errors in the console.

from bge import logic
cont = logic.getCurrentController()
own = cont.owner
text = own["Text"]
length = len(text)
if own["Text"][length-1:length] == chr(10): #if last character is enter
    text = text[0:length-1] #strip last character (removes the line break character)
    if text == "very":
        own.text = "That will be the day."

Python supports relative indexing; To find the last character, use a negative index [-1]. However, because you’re probably looking for endswith() in this instance, and rstrip()

what’s triggering this?

Do i need a all keys keyboard sensor?

Sorry i did not put the correct script in the python controller.
I am getting an error now.

Blender Game Engine Started
Blender Game Engine Finished
Python error compiling script - object 'cube', controller 'And':
  File "Text", line 11
    ^
SyntaxError: unexpected EOF while parsing
import bge
from bge import logic
cont = logic.getCurrentController()
own = cont.owner
text = own["Text"]
length = len(text)
if own["Text"][length-1:length] == chr(10): #if last character is enter
    text = text[0:length-1] #strip last character (removes the line break character)
    if text == "very":

Because your if statement just, well, ends.

You are saying:
if text = ‘very’:

and the computer is going:
yeah? if the text is ‘very’ I do, um, what?

Why does this code not do anything?There is no error in the console.
Could it be that code does not work for blender 2.69?I have python 3.3 installed.

import bge
from bge import logic
cont = logic.getCurrentController()
own = cont.owner
text = own["Text"]
length = len(text)
if own["Text"][length-1:length] == chr(10): 
    text = text[0:length-1] 
    if text == "very":
        own.text = "Okay ,i will."

for debugging add this lines:


import bge
from bge import logic
cont = logic.getCurrentController()
own = cont.owner
text = own["Text"]
length = len(text)

print("001 - text:'",text,"', len:", length) # remove when done

if own["Text"][length-1:length] == chr(10): 

    print("002 - found chr(10)") # remove when done

    text = text[0:length-1] 
    if text == "very":

        print("003 - text:'",text","'") # remove when done

        own.text = "Okay ,i will."

You will see print statement 001, and maybe 002 and more maybe 003 ;).

Hints:
A) You can check the last item of an array (string) with negative index e.g. text[-1].

B) The CR character is "
". You can check for that too :wink: (… == "
").

C) You can split the string with text.split("
"). If the resulting list has more than one entry there was an CR and the first one contains the string you are looking for (the second item should be empty).

D) If you encapsulate confusing code into a function the code gets much more readable:


if endsWithCR(text):
   ...

…while writing this it comes to my mind: why do you expect text that does not end with CR?

I hope it helps

Could you just give me the corrected code.Because that is too hard for me to understand right now.Really i thought it would be easier.

I would suggest reading this. If people just “fix the code” for you, you aren’t really going to have much success with your game. You need to learn WHY it works and HOW it works.

http://swaroopch.com/notes/python/

The problem is, I do not really know what you think the code should do.

This is why I suggested to add the print statements. This helps you to follow the processflow.