This question already has an answer here:
Python - AttributeError: 'int' object has no attribute 'randint'
(1 answer)
Closed 5 years ago.
I am a beginner Python user. I was wondering why I am unable to use two randint() functions. I've done a little research on the issue, and it seems as though "I can only have one random module active at any time". Is there any way to get around this so I can have two "random values"?
Also, this is my first question ever, so criticism towards the question, sample code and answer are all welcome.
import random
random = random.randint(1, 5)
random_2 = random.randint(7, 11)
print(random)
print(random_2)
What's happening is that Python is confusing random (the number) with random (the module). Also, don't name the file random.py because it's going to search the module in the local directory, first.
And it's not a bug.
Related
My code worked, I changed nothing, and now its highlighted saying it cant reference the module properly
ive tried retyping everything, looking up tutorials, trying SUPER simple lines of code, and my random module in pycharm python 3 is not working
import random
value = random.random()
print(value)
basically,
value = random.random() after the dot is highlighted and is saying cannot find reference 'random' in random, like... what??
If there is a space after the dot(random. random() ) remove it.
random.random()
Try it
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 6 years ago.
Improve this question
I am fairly new to the programming world and am wondering why the following code will not only refuse to run, but my python software won't even give me any error messages whatsoever.I'm using Pythonista, an IOS app for python. I can't get the app to run this code (and it won't give me any error messages) and was wondering if it's my code itself, or is it just the application. Any knowledge on this issue would be greatly appreciated.
def starBits():
badMatchups = [Zelda, Cloud, Ryu]
worstMatchups = [Jigglypuff, Villager, Bayonetta]
print(badMatchups)[1:2]
print(worstMatchups)[1:1]
def main():
starBits()
main()
I'm not sure what you expect from this, but it's really funky syntax.
print(badMatchups)[1:2]
print(worstMatchups)[1:1]
If those slices are subscripts for the lists, you need them inside the call to print:
print(badMatchups[1:2])
print(worstMatchups[1:1])
By the way, do you realize that [1:1] is an empty slice? The second number is the first position not included. You may need
print(badMatchups[1:3]) # two elements
print(worstMatchups[1:2]) # one element
Also, are those elements external variables, or are they supposed to be literal names? If the latter, then you have to put them in quotation marks.
badMatchups = ["Zelda", "Cloud", "Ryu"]
worstMatchups = ["Jigglypuff", "Villager", "Bayonetta"]
With this change, the code runs; I hope it's what you want.
Can't get it to run? Reality check time ...
Full code, changes made:
def starBits():
badMatchups = ["Zelda", "Cloud", "Ryu"]
worstMatchups = ["Jigglypuff", "Villager", "Bayonetta"]
print(badMatchups[1:3])
print(worstMatchups[1:2])
def main():
starBits()
main()
Output:
['Cloud', 'Ryu']
['Villager']
This question already has answers here:
Short description of the scoping rules?
(9 answers)
Closed 6 years ago.
I am working through an example Python script from the book "Doing Math with Python" and I keep running up against a NameError which tells me my variable isn't defined, when, it looks to me like it is defined.
I'm using Python 3.4 and the code is
'''
Gravitational Calculations
'''
import matplotlib.pyplot as plt
#Draw the graph
def draw_graph(x,y):
plt.plot(x,y,marker='o')
plt.xlabel('Distance (m)')
plt.ylabel('Force (N)')
plt.title("Gravitational force as a function of distance")
def generate_F_r():
#Generate values for r
r=range(100,1001,50)
#Empty list to store F values
F=[]
#G Constant
G=6.674*(10**-11)
#Two masses
m1=0.5
m2=1.5
#Calculate F and append it into the F-list
for dist in r:
force=G*m1*m2/(dist**2)
F.append(force)
#Call the Draw Plot Function
draw_graph(r,F)
if __name__=='__main__':
generate_F_r()
The error it gives me is:
NameError name 'r' is not defined
Isn't it defined in the line that states r=range(100,1001,50)?
Why isn't it taking this as a definition?
I'm sure there is something glaringly simple and incredibly stupid I'm doing but I am at my wits end as to how such a simple thing could be so hard.
Thanks!
Code in functions is not executed until the function is called. You don't call generate_Fr() until after you have already tried to reference r. Even if you did call the function first, though, r is still just a local variable. You need to make it global with global r at the beginning of the function.
This question already has answers here:
What causes a Python segmentation fault?
(8 answers)
Closed 9 years ago.
How can I run the following program in python 2.7.3
import sys
sys.setrecursionlimit(2 ** 20)
def f(x):
if (x==0): return 0
else: return f(x-1)+1
print f(200000)
This code receives segmentation fault in Ubuntu.
The Python interpreter runs out of stack space. Like any other process in the same situation, it is getting killed by the operating system.
You could try increasing the OS stack size limit (ulimit -c).
A better approach might be to rewrite your code so that it does not require recursion this deep (your particular example can be trivially converted into iteration).
I'm a Highschool Student learning python and I'm a bit stuck on why I am getting an error message in this script. It is supposed to prompt the user for info on how old they are and then return the info in days, hours, and minutes. I'm using the Graphics.py module to accomplish this. The error I am getting is:
how old are you.py", line 17, in <module>
years=entry1.getText()
AttributeError: 'NoneType' object has no attribute 'getText'
I know that the module is properly installed as the getText function works on another script. My code can be seen below. Thanks for any help!
from graphics import*
win=GraphWin('How Old Are You?',250,500)
win.setBackground ('Gray')
entry1= Entry(Point(125,100),10).draw(win)
entry2= Entry(Point(125,200),10).draw(win)
entry3= Entry(Point(125,300),10).draw(win)
Text(Point(125,50),'How many years old are you?').draw(win)
Text(Point(125,150),'What month in the year? (number)').draw(win)
Text(Point(125,250),'How many weeks into the month?').draw(win)
Text(Point(125,25),'When done click outside a box').draw(win)
win.getMouse()
years=entry1.getText()
months=entry2.getText()
days=entry3.getText()
totalDays=(years*365)+(months*30)+(days)
totalHours=((years*365)+(months*30)+(days))*24
totalMinutes=(((years*365)+(months*30)+(days))*24)*60
Text(Point(125,350),totalDays)
Text(Point(125,400),totalHours)
Text(Point(125,450),totalMinutes)
I don't know the graphics library you are using, but your error seems to be trying to accomplish too much at once.
You do:
entry1= Entry(Point(125,100),10).draw(win)
entry2= Entry(Point(125,200),10).draw(win)
entry3= Entry(Point(125,300),10).draw(win)
In each line here, you do create an object - by calling Entry(...), and call a method on that object. The return value of the draw method is what ends up stored in the variables.
Usually, in Python objects, methods won't return their object back. If the method does perform an action (like the name draw ) suggests, it will usually return None - and that is what is happening here, as we see in your error message.
So, all you have to do is to first create your entries, and after that call the draw method on them:
entry1= Entry(Point(125,100),10)
entry2= Entry(Point(125,200),10)
entry3= Entry(Point(125,300),10)
entry1.draw(win)
entry2.draw(win)
entry3.draw(win)
Aside from that, if you don't want your code to be so repetitive, you can create your
entries in a loop and store them in a Python list:
entries = []
for vpos in (100,200,300):
entry = Entry(Point(125,vpos),10)
entries.append(entry)
entry.draw(win)
Text(Point(125,50),'How many years old are you?').draw(win)
Text(Point(125,150),'What month in the year? (number)').draw(win)
Text(Point(125,250),'How many weeks into the month?').draw(win)
Text(Point(125,25),'When done click outside a box').draw(win)
win.getMouse()
years, months, days = (entry.getText() for entry in entries)