Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Randint() missing 1 required positional argument: 'b' . code works if I want to use specific numbers but not if i want to add randomness

import random


list_of_names = []
length_of_list = int(input("give the number of people in your secret santa. make it even"))
if length_of_list % 2 != 0:
    print("that is not even")
    exit()
else: 
    pass

for i in range(length_of_list):
    name = input("give a name")
    list_of_names.append(name)

print(list_of_names)


random_thing = random.randint(len(list_of_names))
print(list_of_names[random_thing],"will have to buy a present for",list_of_names[random_thing])

the error comes up for the last line (23) and i don’t understand why. I am aware that the code is not finished yet but i do not understand as to why it is giving me this error

it works if for example i do list_of_names[0] and list_of_names[1] but when i try and add randomness to the question it doesn’t like it.

At first i tried putting the random function in the last line in the [] but outputted the same problem

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The random.randint() function takes two arguments: a and b, where a is the lower bound and b is the upper bound. So, to generate a random integer in the range of 0 to the length of your list, you can call random.randint(0, len(list_of_names) - 1)

You can fix the last line of your code as follows:

random_index = random.randint(0, len(list_of_names) - 1)
print(list_of_names[random_index], "will have to buy a present for", list_of_names[random_index])

Additionally, the code as it is currently written will always print the same name twice (the name at the random index that was selected). You will need to modify the code to pick two different names from the list, one for the person who is buying the present and one for the person who will receive the present

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading