Code is not correct | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

23rd Jan 2023, 3:48 PM
libre
libre - avatar
10 Answers
+ 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
23rd Jan 2023, 4:04 PM
Lothar
Lothar - avatar
+ 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* ?
23rd Jan 2023, 7:37 PM
Lothar
Lothar - avatar
+ 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
23rd Jan 2023, 4:11 PM
libre
libre - avatar
+ 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
23rd Jan 2023, 5:02 PM
libre
libre - avatar
+ 1
Import the random module; Add this line at the starting of your program: import random
24th Jan 2023, 8:15 AM
Omanshu
Omanshu - avatar
+ 1
And have you imported random module Add to the first line: import random
25th Jan 2023, 8:37 AM
Daniil Pivovarov
Daniil Pivovarov - avatar
0
Just import pdb :)
23rd Jan 2023, 6:25 PM
libre
libre - avatar
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
23rd Jan 2023, 7:49 PM
libre
libre - avatar
0
That means you wanted to select random value from the list , then use (x =random.choice(my list) )
24th Jan 2023, 5:49 AM
Abdul rahmon
Abdul rahmon - avatar
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 #************* :):):) ******************
24th Jan 2023, 7:51 PM
libre
libre - avatar