WorldViz User Forum  

Go Back   WorldViz User Forum > Vizard

Reply
 
Thread Tools Rating: Thread Rating: 7 votes, 5.00 average. Display Modes
  #1  
Old 08-29-2008, 08:40 AM
Elittdogg Elittdogg is offline
Member
 
Join Date: Aug 2007
Posts: 77
String within a string problem?

I'm sure this is very basic, but for whatever reason I'm unable to wrap my head around it. I'm using python based code only.

Code:
x=raw_input("Enter something for x or enter 'quit' to exit the program: ")
y=raw_input("Enter a choice (#, a, or b): ")

if x == 'quit':
    print 'Done.  Thank you.'
else:
    print y

if y.isdigit():
    print x[y]
and this is what happens:
Enter something for x or enter 'quit' to exit the program: hello
Enter a choice (#, a, or b): 2
2

Traceback (most recent call last):
File "C:/Python25/homework 1 Littman.py", line 10, in <module>
print x[y]
TypeError: string indices must be integers


What should come up is l. I need to be able to specify which place in the string the number corresponds to. For instance, (and this works) if I set x='monkey' and y=2. Then I print x[y] I get n. Why doesn't it work when I enter in 2 into the prompt???

So my 3 choices are a number, a or b. If it's a number then I need to output the correct letter from the string (but of course it can't exceed the length of the string). If I enter a, I need to output the length of the string (so hello would be 5 (I also need to start counting from 1, not 0). And if I enter b I need to output the initial string in all CAPS (so I'd need hello to be HELLO). Right now I can't get past the integer and I don't know why.
Reply With Quote
  #2  
Old 08-29-2008, 01:35 PM
farshizzo farshizzo is offline
WorldViz Team Member
 
Join Date: Mar 2003
Posts: 2,849
The raw_input function returns a string object, which cannot be used as an index in a list. You will need to convert the string to an integer first:
Code:
index = int(y)
print x[index]
Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
export problem bazelaisr Vizard 2 05-28-2008 10:19 AM
5DT Data Glove 5 Ultra Problem bjgold Vizard 1 08-08-2006 04:08 PM
problem with female animations vmonkey Vizard 1 10-07-2005 10:36 AM
sound problem alaa Vizard 7 09-02-2005 01:13 PM
problem with input boxes shai Vizard 6 08-15-2005 10:51 AM


All times are GMT -7. The time now is 04:08 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2019, vBulletin Solutions, Inc.
Copyright 2002-2018 WorldViz LLC