"name 'random' is not defined" when using random.randint [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 am writing a Python script that will print out a random value from a list I have written,
from random import randint
class_list=[0,1,2,3,4,5,5,6,7,8,9,10,11,12]
people_called=[]
randomized_number = random.randint(0,12)
print "debug number" + str(randomized_number)
print "The student is" + str(class_list[randomized_number])
class_list[randomized_number].append(people_called)
However when I run this file I get I get
Traceback (most recent call last):
File "./Code/class list.py", line 4, in <module>
number = random.randint(0,12)
NameError: name 'random' is not defined

from random import randint imports randint from random module. That is, you can just use it as randint. If you would import it as import random, you'd have to use random.randint instead.

When importing using just import random, you must call the function with random.randint().
When using from random import randint, Python lets you call the function with just randint()

Related

Python returns None to input [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 1 year ago.
Improve this question
Hey so I’m writing a text-adventure game in python and made a typingprint function to make the text look like it was typed out. But when I try to use this function with inputs it will reply with None.
import random
import time
import console
import sound
import sys
def typingPrint(text):
for character in text:
sys.stdout.write(character)
sys.stdout.flush()
time.sleep(0.05)
console.set_color()
typingPrint('Hello! My name is Sawyer and I will be your guide
throughout this game. Heres a helpful hint, every time you enter
room you will have to battle a enemy.')
time.sleep(3)
player_name = input(typingPrint('\nBefore we begin why don\'t
you tell me your name: '))
How can I fix this?
This is because your function doesn't return anything so there is no text in the input prompt. To solve it, you will need to first call your function, then the input -
typingPrint('Hello! My name is Sawyer and I will be your guide throughout this game. Heres a helpful hint every time you enter a room you will have to battle a enemy.')
time.sleep(3)
typingPrint('\nBefore we begin why dont you tell me your name: ')
player_name = input()

Inheritance not working as tutorial. what is the problem? [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'm studying 'inheritance' in Pycharm.
I just followed tutorial. And it doesn't work. What's the problem
##Chef.py##
class Chef:
def make_chicken(self):
print("The chef makes a chicken")
def make_salad(self):
print("The chef makes a salad")
def make_special_dish(self):
print("The chef makes bbq ribs")
##another.py##
from Chef import Chef
class another(Chef):
##app.py##
from another import another
a = another()
a.make_salad()
run>>>
error message :
Traceback (most recent call last):
File "C:/Users/NEWS1/PycharmProjects/exc/app.py", line 1, in <module>
from another import another
File "C:\Users\NEWS1\PycharmProjects\exc\another.py", line 9
^
SyntaxError: unexpected EOF while parsing
Process finished with exit code 1
What is the problem....
The issue is in your 'another' class, there's nothing following the colon. You can either add methods to the class or just a 'pass' like so
##another.py##
from Chef import Chef
class another(Chef):
# other content
pass

Python code for Debye theory gives a syntax error [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 4 years ago.
Improve this question
I have this code that I typed into Python and I'm getting "Syntax error: invalid syntax" for b=Od/T. Does it have to do with how it's defined? How can I fix it
import scipy.integrate as sci
import scipy.constant as scc
import math
import numpy as np
import matplotlib.pyplot as plt
from IPython import get_ipython
get_ipython().run_line_magic('matplotlib', 'inline')
def f(T):
n=6.022*(10**28)
Od=429
V=10**(-3)
ft=lambda x: ((x**4)*math.exp(x)/(((math.exp(x))-1**2))
b = Od/T
a=0
C=9*V*n((T/Od)**3)*scc.k*(sci.quad(ft,a, b.any(),limit=10))[0]
return C
T1=np.arange(5,500,1)
plt.plot(T1,f(T1),'r-')
You're missing a closing parentheses on the previous line:
ft=lambda x: ((x**4)*math.exp(x)/(((math.exp(x))-1**2))
# ^ This parenthesis is never closed.
Error in the line before that. You can try this.
fr=lambda x: (x**4)*math.exp(x)/((math.exp(x))-1**2)

ImportError: cannot import name 'pyplot' [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 4 years ago.
Improve this question
I'm trying to import pyplot from matplotlib but I get this error:
~/PycharmProject/untitled # jims-mbp (jim)
| => python math.py
Traceback (most recent call last):
File "math.py", line 1, in <module>
import matplotlib.pyplot as plt
File "/Users/jim/PycharmProject/untitled/matplotlib.py", line 1, in
<module>
from matplotlib import pyplot
ImportError: cannot import name 'pyplot'
___________________ | ~/PycharmProject/untitled # jims-mbp (jim)
| =>
I've seen other posts related to this issue but no answers that solve my problem.
this is what I'm running:
import matplotlib.pyplot as plt
plt.plot(range(10))
I guess you run into problems, as your file is called "/Users/jim/PycharmProject/untitled/matplotlib.py" and you have a naming conflict there.
Try to rename it to sth else and rerun.

NameError: name 'random' is not defined [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 1 year ago.
Improve this question
Here is my code:
r = random.randint(1,10)
But for some reason it's giving me the error
NameError: name 'random' is not defined
Other info: Mac, Python, 3.4.0 pylauncher
You have to import the module random:
import random
r = random.randint(1,10)
# ...
>>> import random
>>> r = random.randint(1,10)
>>> r
10
you need to import the random library with the import keyword
import random
random.randint(1,10) #no. between 0 and 10

Categories

Resources