"randint" function working unexpectedly inside functions? [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I need to use the random.randint inside of a function, but it always returns weird results. However, when I try it outside of the function, it works perfectly.
Here's a quick function to choose a number between one and six to illustrate my point:
import random
def dice():
return random.randint(1, 7)
print(dice)
And then I get an output like:
<function dice at 0x0000000004314D90>
I would really appreciate it if you could explain what's happening and how to prevent it.

You need to call the dice function and then print the result (return value).
print(dice())
# ^^
In Python, functions are called by placing (...) after their names. If your function takes any arguments, they would go where the ... is and be separated by commas:
func(arg1, arg2)
Your current code was printing <function dice at 0x0000000004314D90> because that is the representation of the function object that you were printing.

you are simply forgetting a set of parenthesis. it should be
print(dice())
now you are printing the function object.

Related

For this program I am trying to print out the value of the function but it's printing the location in the memory [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
def twoDim(foo):
two_d= []
new = []
for i in range(0,9):
for j in range(0,9):
new.append(foo)
two_d.append(new)
twoDim(0)
print(twoDim)
twoDim is name for a function. You want to see result of its call
print(twoDim(0))
or
result = twoDim(0)
print(result)
Your function also doesn't return anything currently so any result of its call will be None. You need to add return statement with whatever variable you want to be the output of the function.
Your function doesn't have a return statement which returns a value when the function is called so you can add a return statement which returns the value you expect the function to have when using it with the print function.
Also, keep in mind that using only the name of the function without parenthesis like you did in your code doesn't call the function but returns the memory location of your function object.
twoDim is the function name.
So, instead of print(twoDim), you should write print(twoDim(0)) which means calling the function and printing its return value.
In your case, twoDim function doesn't have a return value, so the correct print statement would also not print anything.
If you intend to print two_d and new, you can do that in the function itself and then just call the function OR return something from the function.

The function does not run when I call it, Python [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I want to call a function (my_function), where clearly in my code I am calling it but if the function is not executing.
Someone knows what this is??
Here is my code:
import pandas as pd
my_variable = "1"
if my_variable == "1":
my_function()
global my_function
def my_function():
def tryloc(df, col, idx, default=None):
try:
return df.iloc[col, idx]
except IndexError:
return default
print("hello")
edit = pd.read_csv("priv/productos.csv")
product = tryloc(edit, 1, 0)
print(product)
and the second problem is that it says that this function does not exist when I am declaring it globally
Thanks if you answer!
Despite popular belief, "it's not working" isn't adequate information to diagnose a problem. That said, you are calling the function before it is even declare. You must declare a function before it can be called, always.Try moving
myFunction();
after where you defined it. But, there is a lot more going on here that's wrong and the question doesn't explain what the desired outcome is so I'm not sure how to help. I recommend going back and checking out some python scope documents.

python .apply calling function error [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
def awesome_count():
if 'awesome' in dict:
return 1
else:
return 0
products['awesome']= products['word_count'].apply(awesome_count)
TypeError: awesome_count() takes no arguments (1 given)
what is the issue with calling the function. can somebody help?
Looks like your awesome_count function should take one argument, dict:
def awesome_count(dict):
....
You should really call it something besides dict though as that is a built-in data type. For something simple like this d would be fine.
Another thing to keep in mind is that Python is not javascript -- there is no apply method on dicts unless you have subclassed and added it yourself.

The method list.pop returns None instead of the value intended [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I was just trying out this method .pop() and it says in the docs it is supposed to "Remove the item at the given position in the list, and return it."
So I tried the following code:
def lao(li, i):
guess=input('Have a guess: ')
if guess == li[i]:
li.pop(i)
ho=list('abcde')
I wanted to see if lao(ho, 0) returned and removed 'a' as I thought it would, and this is how it went:
>>> print(lao(ho, 0))
Have a guess: a
None
>>> ho
['b', 'c', 'd', 'e']
So clearly the .pop() method was executed since 'a' was removed but the method didn't return 'a', instead it returned None.
I don't understand why?
Change li.pop(i) to return li.pop(i). Python functions return None by default (when no return statement is present/reached). To return something from the function you must use the return keyword. See a demo on Ideone.
Maybe try return li.pop(i)?
The method is probably working correctly, it's just that you are not returnning an actual value

Python: call a function inside another function [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
regardless of what the functions have to return, my code seems to not call the first function properly, as when I try to pass some doctest it raises the error:
File "preg3.py", line 27, in mesDivisions
if nombreDivisions(llista[0],m)>=nombreDivisions(llista[1],m):
NameError: global name 'nombreDivisions' is not defined
here is my code:
def nombreDivisons(n,m):
x=0
def aux(n,m):
if n<m:
return x
else:
if n%m==0:
x=x+1
return aux(n/m,m)
else:
return x
def mesDivisions(llista,m):
if len(llista)==1:
return llista[0],nombreDivisions(llista[0],m)
else:
if nombreDivisions(llista[0],m)>=nombreDivisions(llista[1],m):
del llista[1]
return mesDivisions(llista,m)
else:
del llista[0]
return mesDivisions(llista,m)
any ideas why?
Check your white space. You want at least one and according to pep8 two blank lines between functions.
You failure is a typo though. It should be nombreDivisions but you left out the i so it is nombreDivisons.

Categories