1

learning python and working with some basic string methods.

I have this program which finds the index of the first lowercase char in a string:

def find_lower(word):
i = 0
while not isupper(word[i]):
    i = i+1
return i

however, when I run this code, I get the error

builtins.NameError: name 'isupper' is not defined

Do I need to import a module or something for isupper() to work?

Mureinik
  • 297,002
  • 52
  • 306
  • 350
mc-lunar
  • 293
  • 2
  • 4
  • 14
  • 5
    is upper is a method of string. It must be called on a string like `word[i].isupper()`. – Paul Rooney Jun 13 '18 at 06:02
  • 3
    Instead of racing to answer and aiming for reps, OP should be directed to the documentation page for his sake. This problem can be found on web with just a few clicks, they should be encouraged for research and exploration. – BcK Jun 13 '18 at 06:06

4 Answers4

4

You are using isupper incorrectly. It is not a built in standalone function. It is a method of str. The documentation is here.

For any function that you are unsure of you can consult the python documentation. It is well indexed by google so a google search is a good place to begin when you want to understand how to use a function.

Once that is out of the way, your find_lower has a few more issues. It actually finds the index of the first upper case letter due to a logic error.

while not word[i].isupper():

Continues to loop if the character is not upper case and stops if it is. So you need to remove the not.

def find_lower(word):
    i = 0
    while word[i].isupper():
        i = i+1
    return i

print(find_lower('ABcDE')) # prints 2

The next error is that if there are no lowercase characters it walks off the end of the string and throws an exception

>print(find_lower('ABCDE'))
Traceback (most recent call last):
  File "./myisupper.py", line 11, in <module>
    print(find_lower('ABCDE'))
  File "./myisupper.py", line 5, in find_lower
    while word[i].isupper():
IndexError: string index out of range

To fix this you need to limit the number of iterations to the length of the string, this is left as an exercise to fix.

Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
3

isupper is a method of string. so, you should use it on a string

For example,

a = "Hello"

#You can check upper on this as follows
print(a.isupper())

In your case, change the following

while not isupper(word[i]):

to

while not word[i].isupper():
    print("Your x here")
rawwar
  • 4,834
  • 9
  • 32
  • 57
  • Got it, thanks! How do I know when I am supposed to use a method *on* something vs putting something in the parens (like for print(x) for example). – mc-lunar Jun 13 '18 at 06:07
  • @mc-lunar The updated answer does not contain any information regarding to your question. If you want to find answer to your question, you need to understand the difference between a method and a function. Here: https://stackoverflow.com/q/155609/4237254 – BcK Jun 13 '18 at 07:35
2

isupper is a method of string, and should be called by that object: word[i].isupper().

Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • Thank you. How do I know when something is supposed to go before the method and when something is supposed to go in parens (eg : print(i) ) – mc-lunar Jun 13 '18 at 06:06
1

it should be like

def find_lower(word):
  i = 0
  while not word[i].isupper():
      i = i+1
  return i
Nihal
  • 5,262
  • 7
  • 23
  • 41