Question: Language is Python I need help with this code. This is a gift exchange python program, I need to add to the starter code below
Language is Python
I need help with this code. This is a gift exchange python program, I need to add to the starter code below to make sure that nobody is assigned to buy a gift for themselves, and each person can only give one gift and can only receive one gift (no repeats). The program keeps trying (looping) until both of these conditions are met. The program should work every time regardless of the number of people participating.
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Code:
# Gift-assigner program
import random
numPeople = int(intput("How many people are playing? "))
givers = []
recivers = []
print("Please enter their names: ")
# Get all of the players' names and add them to the list
for i in range(numPeople):
givers.append(input(""))
# Randomly assign gift givers to gift receivers. Check to make sure that nobody is assigned themselves (which is no fun!), and that each person can only give one gift and can only receive one gift (no repeats). Keep trying (looping) until everyone is giving a gift to someone else.
for j in range(numPeople):
recivers[j] = random.sample(givers, numPeople)
# Print results
print()
print("Gift Assignments...")
for k in range (numPeople):
print(givers[k], "will buy a gift for," recivers[k])
print()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
