0

I'm trying to enter values from the tuple ('a',1), ('b',2),('c',3) into the function dostuff but i always get a return of None or False. i'm new to this to i'm sorry if this question is basic. I would appreciate any help.

I expect the result of this to be:

a1---8
b2---8 
c3---8

Code:

def dostuff(stri,numb,char):
        cal = stri+str(numb)+'---'+str(char)
        return cal

def callit (tups,char):
    for x in range(len(tups)):
        dostuff(tups[x][0],tups[x][1],char)

print(callit([('a',1), ('b',2),('c',3)],8))
2
  • callit returns none because you have no explicit return Commented Oct 14, 2016 at 14:40
  • since you wrote print(callit()) I suppose callit should return a string. What should it return? Commented Oct 14, 2016 at 14:42

4 Answers 4

4

I think you're misunderstanding the return value of the functions: unless otherwise specified, all functions will return None at completion. Your code:

print(callit([('a',1), ('b',2),('c',3)],8))`

is telling the Python interpreter "print the return value of this function call." This isn't printing what you expect it to because the callit function doesn't have a return value specified. You could either change the return in your dostuff function like so:

 def dostuff(stri,numb,char):
    cal = stri+str(numb)+'---'+str(char)
    print cal

def callit (tups,char):
    for x in range(len(tups)):
        dostuff(tups[x][0],tups[x][1],char)

callit([('a',1), ('b',2),('c',3)],8)

This changes the return on the third line into a print command, and removes the print command from the callit call. Another option would be:

 def dostuff(stri,numb,char):
    cal = stri+str(numb)+'---'+str(char)
    return cal

def callit (tups,char):
    for x in range(len(tups)):
        cal = dostuff(tups[x][0],tups[x][1],char)
        print(cal)


callit([('a',1), ('b',2),('c',3)],8)

This takes the return value from the dostuff function and stores it in a variable named cal, which could then be printed or written to a file on disk.

Sign up to request clarification or add additional context in comments.

Comments

1

as @n1c9 said, every Python function must return some object, and if there's no return statement written in the function definition the function will implicitly return the None object. (implicitly meaning that under the hood, Python will see that there's no return statement and returnNone)

However, while there's nothing wrong in this case with printing a value in a function rather than returning it, it's generally considered bad practice. This is because if you ever want to test the function to aid in debugging, you have to write the test within the function definition. While if you returned the value you could just test the return value of calling the function.

So when you're debugging this code you might write something like this:

def test_callit():
    tups = [('a', 1), ('b', 2), ('c', 3)]
    expected = 'a1---8\nb2---8\nc3---8'
    result = callit(tups, 8)
    assert result == expected, (str(result) + " != " + expected)

if you're unfamiliar with the assert statement, you can read up about it here

Now that you have a test function, you can go back and modify your code. Callit needs a return value, which in this case should probably be a string. so for the functioncallit you might write

def callit(tups, char):
    result = ''
    for x in range(len(tups)):
        result += dostuff(tups[x][0], tups[x][1], char) + '\n'
    result = result[:result.rfind('\n')] # trim off the last \n
    return result

when you run test_callit, if you get any assertion errors you can see how it differs from what you expect in the traceback.

What I'm about to talk about isn't really relevant to your question, but I would say improves the readability of your code.

Python's for statement is very different from most other programming languages, because it actually acts like a foreach loop. Currently, the code ignores that feature and forces regular for-loop functionality. it's actually simpler and faster to write something like this:

def callit(tups, char):
    result = ''
    for tup in tups:
        result += dostuff(tup[0], tup[1], char) + '\n'
    result = result[:result.rfind('\n')] # trim off the last \n
    return result

Comments

0

For a function to return a value in python it must have a return statement. In your callit function you lack a value to return. A more Pythonic approach would have both a value and iterate through the tuples using something like this:

def callit(tups, char):
    x = [dostuff(a, b, char) for a, b in tups]
    return x

Since tups is a list of tuples, we can iterate through it using for a, b in tups - this grabs both elements in the pairs. Next dostuff(a, b, char) is calling your dostuff function on each pair of elements and the char specified. Enclosing that in brackets makes the result a list, which we then return using the return statement.

Note you don't need to do:

x = ...
return x

You can just use return [dostuff(a, b, char) for a, b in tups] but I used the former for clarity.

Comments

-1

You can use a list comprehension to do it in one line:

char = 8
point_list = [('a', 1), ('b', 2),('c', 3)]
print("\n".join(["{}{}---{}".format(s, n, char) for s, n in point_list]))

"{}{}---{}".format(s, n, char) creates a string by replacing {} by each one of the input in format, therefore "{}{}---{}".format("a", 1, 8) will return "a1---8"

for s, n in point_list will create an implicit loop over the point_list list and for each element of the list (each tuple) will store the first element in s and the second in n

["{}{}---{}".format(s, n, char) for s, n in point_list] is therefore a list created by applying the format we want to each of the tuples: it will return ["a1---8","b2---8","c3---8"]

finally "\n".join(["a1---8","b2---8","c3---8"]) creates a single string from a list of strings by concatenating each element of the list to "\n" to the next one: "a1---8\nb2---8\nc3---8" ("\n" is a special character representing the end of a line

3 Comments

It's not very helpful to give a one-liner without explaining it to a user who is confused about the return value of a function.
How does this help the OP? He is confused about function calling and returning. he didn't ask for a more concise way to do it.
@VincentSavard I edited my answer to explain each step

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.