#1
|
|||
|
|||
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] 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. |
#2
|
|||
|
|||
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] |
|
|
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 |