How do I print a function return?
My code looks like this:
from random import randint Item = randint(1, 10) def ItemName(Item): if Item == 1: return Hat elif Item == 2: return Gloves elif Item == 3: return Scarf elif Item == 4: return Top elif Item == 5: return Pants elif Item == 6: return Shoes elif Item == 7: return Socks elif Item == 8: return Sunglasses elif Item == 9: return Bag else: return Jacket print (ItemName)
I expect the program to print one of the possible values it could return. Like, 'hat', or 'socks', or 'bag'. Instead, I get "" or some other variant of complete gibberish.
You never invoked the function with the Item, you just printed the Function. you have many undefined variables, from the context I think it should be a string Try the following
from random import randint Item = randint(1, 10) def ItemName(Item): if Item == 1: return "Hat" elif Item == 2: return "Gloves" elif Item == 3: return "Scarf" elif Item == 4: return "Top" elif Item == 5: return "Pants" elif Item == 6: return "Shoes" elif Item == 7: return "Socks" elif Item == 8: return "Sunglasses" elif Item == 9: return "Bag" else: return "Jacket" print (ItemName(Item))
Why would you use the return statement in Python?, The print() function writes, i.e., prints, a string or a number on the console. The return statement does not print out the value it returns when the The print() function writes, i.e., "prints", a string or a number on the console. The return statement does not print out the value it returns when the function is called. It however causes the function to exit or terminate immediately, even if it is not the last statement of the function.
The items within the function must be defined as strings. Don't forget to include the parameters when calling functions.
Hope this helps!
from random import randint item = randint(0, 9) def itemName(item): arr = ["Hat", "Gloves", "Scarf", "Top", "Pants", "Shoes", "Socks", "Sunglasses", "Bag", "Jacket"] return(arr[item]) print(itemName(item))
Python Tutorial: Functions, A return statement ends the execution of the function call and "returns" the result, i.e. return (T_in_celsius * 9 / 5) + 32 for t in (22.6, 25.8, 27.3, 29.8): print(t, ": " In the above program, the function greetUser() accepts multiple parameters of type String and Int. The function also returns multiple values as a tuple of type String and Int. To access each return value, we use index positions 0, 1, … Here, we've used msg.0 to access Good Morning!Jack and msg.1 to access 0.
Functions in Python
To explain what you have done wrong you need a comprehensive understanding of functions.
When you define the function i.e.
ItemName is the name of the function, and
item inside the brackets is the argument. The argument/parameter is simply a variable that can be used within the scope of the code.
For instance given a simple function:
def example(arg): return argument
you pass in the argument
arg, which is anything you pass the function when it is invoked. To properly call/invoke a function:
foo = example('I just called a function!')
This will pass into the function the string
'I just called a function!'. On the function call, it returns the string that you passes into it. To print the string simply print the return of the function:
print(foo) >>> I just called a function!
To fix your code you simply need to pass Items into ItemName like such:
A Better Approach
I have noticed your code is a bit repetitive and in effect unreadable. A better approach to solving the problem would involve using list.
import random ITEMS = ["Hat", "Gloves", "Scarf", "Top", "Pants", "Shoes", "Socks", "Sunglasses", "Bag"] def itemNames(index): if index < len(items): return items[index] return 'Jacket' print(itemNames(random.randint(1,9), items))
The function above will print the index of items list, for instance, if you were to pass in 0, the function would get the first element in the list
Although this way is quite slow as every time you call the function is has to look through each item until it matches the index you provided. A More efficient solution would be to use dictionaries like
If you want a quick one liner you could also consider an anonymous function, or lambda expression as they are called in python
arr_num = lambda x: arr[x] if x < len(arr) else "Jacket" print(arr_num(random.randint(0, 9))
Also your variable and function names shouldn't be capitalised, this is reserved for classes. I would advice you take a brief look at the PEP8 Styling Guide.
Hope this has helped you gain a basic understanding of function use in python, and fixed your code :)
What's the difference between the print and the return functions , print just shows the human user a string representing what is going on inside the computer. The computer cannot make use of that printing. return is how a function Adjust your total function and assign the value that sums returns to a variable, in this case response for more clarity on the difference to the variable result defined in the scope of the sums function. Once you have assigned it to a variable, you can print it using the variable.
from random import randint item = randint(0, 9) def itemName(item): l=["Hat", "Gloves", "Scarf", "Top", "Pants", "Shoes", "Socks", "Sunglasses", "Bag", "Jacket"] try:return l[item] except:return 'index not in list' print(itemName(item))
Is it bad style to return a print statement? - Python, def method(): # stuff return print(“some string”). This is no different that returning any other method and argument, but I don't see it very often in The function calls on the line are run first, and their return values substituted for the function calls, before the line itself is then executed. Active learning: our own return value function. Let's have a go at writing our own functions featuring return values. First of all, make a local copy of the function-library.html file from GitHub.
This is the use of a dictionary, where you call the value by a key:
from random import randint xx =["Hat", "Gloves", "Scarf", "Top", "Pants", "Shoes", "Socks", "Sunglasses", "Bag", "Jacket"] dict(zip(range(1,len(xx)+1),xx)).get(randint(1,11))
Every time you run the code above you will get a different value.
Or you can do:
Item =randint(1,11) vv = dict(zip(range(1,len(xx)+1),xx)) vv.get(Item)
This is exactly the same as the line of code above
6.2. Functions that Return Values, Furthermore, functions like range , int , abs all return values that can be used to build more complex Then look at what is printed when the function returns. By John Paul Mueller . Functions can display data directly in Python or they can return the data to the caller so that the caller can do something more with it. In some cases, a function displays data directly as well as returns data to the caller, but it’s more common for a function to either display the data directly or to return it to the caller.
Python Return Statements Explained: What They Are and Why You , print(no_expression_list()) None. If a return statement is reached during the execution of a function, the current function call is left at that point: > The print () function prints the specified message to the screen, or other standard output device. The message can be a string, or any other object, the object will be converted into a string before written to the screen. print (object (s), separator= separator, end= end, file= file, flush= flush ) Parameter Values.
Python return statement, If the function doesn't have any return statement, then it returns None . def print_something(s): print Another benefit of print () being a function is composability. Functions are so-called first-class objects or first-class citizens in Python, which is a fancy way of saying they’re values just like strings or numbers. This way, you can assign a function to a variable, pass it to another function,