user1695993
user1695993

Reputation: 19

Nested statements Python coding

i need help, theres something wrong in my coding and cannot seem to figure out what..im sure its a simple error its just i cant seem to find it at the moment

    string = raw_input("Enter String->")
    length = len(string)
    index = 0
    while index < length:
       if string(index) == 'a':
            print "Character found is a"
       index = length
       elif string(index) != 'a':
            print "Character", index"is not an a, sorry"

The error of course is "invalid syntax"

This is the new code so far

   string = raw_input("Enter String->")
   length = len(string)
   index = 0
   while index < length:
       if string[index] == 'a':
           print "Character found is a"
           index = length
       elif string[index] != 'a':
           print "Character", index,"is not an a, sorry"

from here when i run the code, it runs infinitely saying "character 0 is not a"

3rd Edit

    string = raw_input("Enter String->")
    length = len(string)
    index = 0
    while index < length:
        if string[index] == 'a':
            index += 1
            print "Character found is a"
            break
        elif string[index] != 'a':
            print "Character", index, "is not an a, sorry"

Upvotes: 0

Views: 167

Answers (6)

toxotes
toxotes

Reputation: 1464

First, really quick word of advice -- using variables with names like 'string' will lead to headaches.

So, the code you've written tells me you need to see that each character position prior to the first 'a' but not its actual value, that you need to know whether 'a' is in the string, and that you do not care about any character at all after the first 'a'. These goals seem a little unlikely to me, so I suspect your real question is slightly different than what you've asked how to do.

I really see two root questions here:

1. How do I find the index of the first 'a' in a given string? If this is your main concern, all you need to do is

response = raw_input('Prompt: ')
index_a = response.find('a')

foo.find(bar) will give you the index of bar, or -1 if it's not in foo at all. But if you don't even care where bar is, and just need to verify that it's there, use bar in foo instead.

2. How do I examine each character in a string? Python doesn't make you build your iteration in many cases -- it gives you the for structure, which does the gruntwork for you. This example will let you keep track of the pre-matched character positions, if you really need that, but note that counter has nothing at all to do with the flow control:

response = raw_input('Prompt: ')
counter = 0
for r in response:
    if r is 'a':
        print('We found it.')
        break
    else:
        print('Not ' + str(counter))
        counter += 1

Upvotes: 0

K Z
K Z

Reputation: 30483

You need to increment index at after each loop, otherwise the while loop will run forever on the same index:

string = raw_input("Enter String->")
length = len(string)
index = 0
while index < length:
    if string[index] == 'a':
       print "Character found is a"
       break
    else:
       print "Character" + str(index) + " is not an a, sorry"
       index += 1

Though, you should study @gnibbler's suggestion and use it instead of while loop for this specific purpose.

Upvotes: 0

Jakob Bowyer
Jakob Bowyer

Reputation: 34718

print "Character", index"is not an a, sorry"

Should read

print "Character " + str(index) + " is not an a, sorry"

Upvotes: 0

Blender
Blender

Reputation: 298432

You've got a few problems:

  • Use square brackets to access elements in an iterable: a[1],
  • Move index = length up one level. It is breaking your loop by being between an if and an elif.
  • print is missing one comma.

A better way to do it would be like this:

string = raw_input("Enter String->")

for index, character in enumerate(string):
   if character == 'a':
        print "Character found is a"
        break
   else:
        print "Character", index, "is not an a, sorry"

Upvotes: 1

John La Rooy
John La Rooy

Reputation: 304393

index = length is going to jump straight to the end of the string. You'd need to use index += 1 instead if you want to check each character

Normally you would write the loops like this

string = raw_input("Enter String->")
for idx, c in enumerate(string):
   if c == 'a':
        print "Character found is a"
   else:
        print "Character", idx, "is not an a, sorry"

Which is easier to read and makes it much harder to make such errors

Upvotes: 2

rantanplan
rantanplan

Reputation: 7450

Index access is accomplished with [i], not with (i), which denotes a function call.

So you should do:

if string[index] == 'a':

Also take a note of the rest of the answers about other problems with your code.

Upvotes: 1

Related Questions