Home > front end >  Randint() missing 1 required positional argument: 'b' . code works if I want to use specif
Randint() missing 1 required positional argument: 'b' . code works if I want to use specif

Time:12-08

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

CodePudding user response:

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

  • Related