Using random library functions in my own functions python 3 [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
Why cant I use randrange in this function?
import random
print (random.randrange(0,4))
def random(dice):
dice_one = random.randrange(1,3)
return (dice_one)
print (random(4))
This is the error
line 6, in random
dice_one = random.randrange(1,3)
AttributeError: 'function' object has no attribute 'randrange'
Thanks for any help

Python thinks you're trying to call a method of your own random function since you named it the same as the random module. Change your function name to something else and it should be fine.
def chance(dice):
dice_one = random.randrange(1,3)
return dice_one
print (chance(4))
Also, not sure what you're trying to do with the 'dice' argument. If this is meant to be the number of sides on the die, try:
def chance(dice):
dice_one = random.randrange(1,dice)
return dice_one

funny ,actually I am not able to understand which kind of output do you expect but from whatever I have understood the solution of program is this !
import random
print random.randrange(0,4)
def random_number(dice):
dice_one = random.randrange(1,3)
return dice_one
print random_number(4)

Related

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.

"randint" function working unexpectedly inside functions? [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 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.

Why does python keep saying the return statement is outside of the 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 8 years ago.
Improve this question
single_char = {}
for item in lines:
for index in range(len(item)):
if item[index - 1] in space:
if item[index + 1] in space:
if item[index] in alplist:
char = item[index]
single_char[char] = index
return single_char
else:
continue
else:
continue
else:
continue​
This is my function by for some reason Python keeps telling me that the return statement "return single_char" is outside of the function. Can somebody please explain how i can fix this problem.
There is no function. And return statement should be always inside a function.
Use def keyword to define a function in python.
Example:
def sum(a, b):
return a+b

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.

Making a Python 2.7 module: Referencing values? [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'm making a fairly simple weather module for myself and I have run into a major issue, I can't get a value from my module.
First, let me show you what I have so I can point out my issue.
PyWeather.py:
import urllib2
import json
import time
class get:
def __init__(self, location):
self.location = location
def status(self):
input = self.location
fixedinput = input.replace(" ","%20")
response = urllib2.urlopen('http://api.openweathermap.org/data/2.5/weather?q=' + fixedinput)
data = json.load(response)
weather = data['weather'][0]['main']
return weather
Main.py:
import PyWeather
location = 'Lexington, SC'
current = PyWeather.get(location).status
print current
I'm a little more than a beginner in Python, but I taught myself, so I don't understand quite a bit.
My issue lies in the output:
<bound method get.status of <PyWeather.get instance at 0x01925940>>
How do I get an output such as 'Clouds' (Which is the current condition)
You are not calling the function. You are just creating an alias to the function using the current variable name. Try
current = PyWeather.get(location).status() # Notice the ()
current = PyWeather.get(location).status()
should fix the issue

Categories