I have multiple function that instead of returning they print a certain string I can’t put the whole function but that’s what they end up doing I want to take all of these function and make them into a single string.
Here’s an example of what one function would print and what I tried
def print_names(lst):
print(name)
def print_id(lst):
print(id)
lst = [name, not_name, not_name,id]
print_id(lst) print_name(lst)
Doing this I get the error unsupported operand types for : none type
CodePudding user response:
ok you should do it like this
def print_names(lst):
return lst[0]
def print_id(lst):
return lst[3]
lst = [name, not_name, not_name,id]
id_and_name = print_names(lst) print_id(lst)
after that you will be able to print the "id_and_name" together
CodePudding user response:
If you tried to return the Values, it would make it easier
def print_names(lst):
#rest of the function
return name
def print_id(lst):
#rest of the function
return id
lst = [name, not_name, not_name,id]
print(print_id(lst) print_names(lst))
CodePudding user response:
Functions return None
, unless stated otherwise. print_id
and print_name
are returning both returning None
and you are trying to add None None
which is not possible.
You can just return strings, concatenate (join) the two strings, and print that new string:
def get_name(lst):
return str(lst[0])
def get_id(lst):
return str(lst[3])
print(get_name(lst) " " get_id(lst))
or f-strings:
print(f"{get_name(lst)} {get_id(lst)}")
Or, use a dictionary so you dont need to index the data:
user_data = {
"name": "tom"
"id": 123456
}
print(f"{user_data["name"]} {user_data["id"]}")
CodePudding user response:
You are getting this error because you are trying to add together the returned value for your print_names
and print_id
methods.
Since there is no return statement in these methods, the return value will be None
and you cannot apply the
operator to the none type.
If you want to print these two items in your list together as one string, you would need to return the items, add them together, and print that:
lst = ['name', 'not_name', 'not_name','id']
def get_name(lst):
return lst[0]
def get_id(lst):
return lst[3]
print get_id(lst) get_name(lst)