Frage

I'm relatively new to Python. I'm working on a script that will reassign a numerical representation of a digit found in a string to its alphabetical counterpart. Because the function is relatively large in size, I'm adding it as a module to the script.

I'm having some issues getting the object back from the imported function. I would like to use the returned object, but it seems to be coming back as None. I've looked into global variables, but I don't know if that's the right direction.

Here is what I've been working with:

...
import numassign
for i in CharacterKey: # i is '0'
    if i.isdigit():        
        FoundInt = numassign.NumberAssignment(input = i)
        CharacterKeyList.append(FoundInt)
        raw_input('{} reassigned to {}'.format(FoundInt, i))
    else:
        CharacterKeyList.append(i)
...

Here is the referenced module (numassign):

...
def NumberAssignment(input):
    if input == 0:
        FoundInt = 'Zero'
        return FoundInt
...

Currently returning FoundInt as None.

None reassigned to 0

How can I cross-reference objects from a module function? I'd rather not clutter up my code with functions if I could import them from a referenced module.

War es hilfreich?

Lösung

You are testing for an integer, put are passing in a string.

They may print the same, but integers and strings never test as equal in Python. Test for the string instead:

if input == '0':

Because your if input == 0 fails for input = '0', your function never reaches a return statement, leaving Python to return the default None instead.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top