Siretoe
Siretoe

Reputation: 29

python syntax error in function

I am a beginner to python, and I have a problem when I was trying my new program. I was trying to define a function to lowercase the inputs, but it only work with integers but int with letters, nor letters, here's what i get:

def SomeString(string):
    lowcase = str(string)
    lowcase.lower()
    print lowcase

Only integers work, integers with letters or letters wont work:

>>> SomeString(TEST0110)
SyntaxError: invalid syntax

and

>>> SomeString(TESTString)

Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    SomeString(TESTString)
NameError: name 'TESTString' is not defined

I tried not to use function to do this and it worked fine:

>>> String = "TEST0110"
>>> String.lower()
'test0110'

I don't know why it won't work with function, please help.
THanks.

Upvotes: 1

Views: 492

Answers (2)

John La Rooy
John La Rooy

Reputation: 304255

SomeString(TEST0110) isn't a syntax error, It's a NameError if you don't have a variable called TEST0110

SomeString(0110TEST) is a syntax error

This is because it 0110 is a number, but followed by garbage

SomeString("TEST0110") is probably what you mean. It passes a string to the function

If you make sure you always passing a str you don't need to call str()

def SomeString(my_string):
    lowcase = my_string.lower()
    print lowcase

Upvotes: 3

avasal
avasal

Reputation: 14854

do this SomeString("TEST0110") instead of SomeString(TEST0110)

When you write SomeString(TEST0110) the code assumes TEST0110 is a variable, but in your case it is not

the quotes "" indicate it is a string

the error NameError: name 'TESTString' is not defined means your code is tring to find the variable with name TESTString

the function .lower() returns the output, which your are not capturing...

Upvotes: 3

Related Questions