==== Opdracht: Wie betaalt de rekening (russisch roulette): ====
# Import the "random" module
import random
# Ask for input: which people to make a choice from
people = input("Who is present? Seperate names using comma and space.\n")
# Make a list from the variable string people:
people_list = people.split(", ")
# Determine number of people participating:
length = len(people_list)
# Select a random number based on the number of people (variable "length"):
choice = random.randint(0, length - 1)
# Get the name from the list belonging to the number generated by choice:
victim = people_list[choice]
# Print the name of who is going to have to pay the bill today:
print(victim, " is going to buy the meal today!")
==== Russisch roulette versie: ====
import random
people = input("Give the names of the people that participate in this game of Russian Roulette, seperated by comma and space:\n")
people_list = people.split(", ")
numbers_of_users = len(people_list)
number = random.randint(0, numbers_of_users - 1)
print(people_list[number] + " is dead!")
In plaats van //random.randint// kun je ook gebruik maken van de //random.choice// optie die sneller is (maar niet gebruikt mocht worden in de opdracht). Dan zou de code er als volgt uitzien:
import random
people = input("Give the names of the people that participate in this game of Russian Roulette, seperated by comma and space:\n")
people_list = people.split(", ")
victim = random.choice(people_list)
print(victim + " is dead!")