0

Please help me find the error in the code :

# Use an import statement at the top import random word_file = "text.txt" word_list = [] #fill up the word_list with open(word_file,'r') as words: for line in words: # remove white space and make everything lowercase word = line.strip().lower() # don't include words that are too long or too short if 3 < len(word) < 8: word_list.append(word) # Add your function generate_password here # It should return a string consisting of three random words # concatenated together without spaces def generate_password(): a=random.randint(len(word_list)) c=random.randint(len(word_list)) b=random.randint(len(word_list)) return word_list[a]+word_list[b]+word_list[c] # test your function print(generate_password())

27th Dec 2018, 2:13 AM
Balaji Pathange
Balaji Pathange - avatar
2 Answers
+ 2
random.randint() needs two parameters (lower bound and upper bound), so randint(len(word_list)) is wrong. You need to use random.randint(0, len(word_list)-1) instead. You could replace the function body of generate_password() with return ''.join(random.choices(word_list, k=3))) and get the same result. If you want to make sure that three different words are used, use return ''.join(random.sample(word_list, 3)).
27th Dec 2018, 6:28 AM
Anna
Anna - avatar
+ 1
What kind of error are you getting? And could you please write this code in the Python codeplayground and just share its link over here bcz no one wants to write the entire code all over again and copy-paste from here will result in indentation error. One thing I can say from the above code is that 'generate_password()' may not return a string made with three distinct words everytime bcz 'random.randint(len(word_list)) may return same number. This problem can be tackled by removing the selected word everytime from the word_list.
27th Dec 2018, 3:24 AM
ŠØŠ°Ń‰Šø Š Š°Š½Š¶Š°Š½
ŠØŠ°Ń‰Šø Š Š°Š½Š¶Š°Š½ - avatar