Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 List Overlap Trouble erfanakbari1 Programmer named Tim Posts: 16 Threads: 12 Joined: Sep 2018 Reputation: 0 Likes received: 0 #1 Mar-18-2019, 12:47 PM Hey Guys! I am given an exercise to solve . Its about list overlap and random generating of lists and to return a list that contains only the elements that are common between the lists . I am done with this part and I succeeded in it . but about this extra part : Randomly generate two lists to test this , I have tried many ways doing this but unfortunately I stuck into trouble . ```import random def RAND(start, finish, num): third_list = [] for c in range(num): third_list.append(random.randint(start, finish)) return third_list num = 5 start = 1 finish = 50 print(RAND(start, finish, num)) def RAND2(strt, fin, n): fourth_list = [] for m in range(n): fourth_list.append(random.randint(strt, fin)) return fourth_list n = 4 strt = 0 fin = 10 print(RAND2(strt, fin, n)) for char in third_list: for char2 in fourth_list: if char == char2: print('This value ** {0} ** of random lists are the same ! '.format(char)) ``` This is my code which includes two random lists and a for loop for taking same elements in both of them . But at line 32 and 33 I got an error while defining name of random lists which I defined earlier . So , I hope that you can help me get along with this code finally . Thanks ichabod801 Bunny Rabbit Posts: 3,361 Threads: 70 Joined: Sep 2016 Reputation: 176 Likes received: 1030 #2 Mar-18-2019, 02:36 PM You need to assign the return value of a function in the module scope if you want to use it there: ```third_list = RAND(start, finish, num) print(third_list) ```It looks like your two rand functions are pretty much the same, except for the confusion about how names work (names created in the function are only available in the function). So you could just do this: ```third_list = RAND(start, finish, num) fourth_list = RAND(start, finish, num) ``` Craig "Ichabod" O'Brien - xenomind.com I wish you happiness. Recommended Tutorials: BBCode, functions, classes, text adventures « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post List Overlap Josh_Python890 4 210 Jun-12-2019, 03:23 PM Last Post: kotter Trouble figuring out how to pass a value between functions? Reta 1 203 Mar-28-2019, 06:34 AM Last Post: Yoriz Trouble with return Rudy 6 325 Mar-03-2019, 07:58 PM Last Post: Rudy Trouble with .after() Tkinter purpleicetwice 5 252 Mar-02-2019, 10:07 PM Last Post: purpleicetwice Trouble in lists erfanakbari1 2 230 Feb-26-2019, 08:46 AM Last Post: perfringo making a dictionary from a list, one key with multiple values in a list within a list rhai 4 601 Oct-24-2018, 06:40 PM Last Post: LeSchakal Trouble with "Weather Program" Assignment sarah_mb_sues 5 1,997 Aug-10-2018, 02:29 AM Last Post: ichabod801 Trouble with edX Python Final sarah_mb_sues 11 3,242 Jun-19-2018, 10:36 AM Last Post: cryomick Trouble with assigning a string value in conditional statement juliabrushett 1 573 Jun-16-2018, 06:21 PM Last Post: ichabod801 I'm having trouble with a test case using python kekersqueaker 1 1,170 Nov-05-2017, 12:13 AM Last Post: sparkz_alot

Forum Jump:

Users browsing this thread: 1 Guest(s)