+ 3
Code is not correct
Hi to all myList = ['USMA', 'MCA', 'JSK', 'ESS', 'CRB', 'MCO'] x=random.sample(myList) print('My favorite club is ' + x[1]) getting error random not difinied Traceback (most recent call last): File "299916335/source.py", line 2, in <module> x=random.sample(myList) NameError: name 'random' is not defined Any suggestion is welkom thanks in advance Regards
10 ответов
+ 11
libre ,
in general you have to read the error messages and follow them. they are pointing to the found issues!
here the code with some comments:
import random # module random has to be imported
myList = ['USMA', 'MCA', 'JSK', 'ESS', 'CRB', 'MCO']
x=random.sample(myList, 1) # sample() is missing the *k* argument, which is the number of elements to take
print('My favorite club is', *x) # output has to like this
+ 7
libre ,
> what the code of your last post is doing what we call *hardcoded*. the result will always be the same.
> but may be you wanted to use a random index number and use this to select an element from the list *myList* ?
+ 2
Thanks Lothar for your replay and the explination and is true the programma is giving answer not at all but is the beginning to solve any code
I did this print('My favorite club is ' + x[1]) zo i want the answer to be MCA ,First of mij list
and also want to add a function that cals pdb
Regards
+ 1
It seem have to be like this
myList = ['USMA', 'MCA', 'JSK', 'ESS', 'CRB', 'MCO']
print('My favorite club is', myList[1]) # output has to like this
##uitput
My favorite club is MCA
i wil try to add a function that calls pdb
Regards
+ 1
Import the random module;
Add this line at the starting of your program:
import random
+ 1
And have you imported random module
Add to the first line:
import random
0
Just import pdb :)
0
Hi
here is the code,not using import random
myList = ['USMA', 'MCA', 'JSK', 'ESS', 'CRB', 'MCO']
print('My favorite club is', myList[1]) # output has to like this
##uitput
My favorite club is MCA
0
That means you wanted to select random value from the list , then use (x =random.choice(my list) )
0
thanks Abdul
i want that the answer must be 1 of myList means MCA
Zo my code is okwas tested it and no error plus giving the right answer
myList = ['USMA', 'MCA', 'JSK', 'ESS', 'CRB', 'MCO']
print('My favorite club is', myList[1]) #
##uitput
My favorite club is MCA
#************* :):):) ******************