# Split string method
names_string = input("Give me everybody's names, separated by a comma. ")
names = names_string.split(", ")
# ? Don't change the code above ?
#Write your code below this line ?
import random
num_items = len(names)
random_choice = random.randint(0, num_items - 1)
person_who_will_pay = names[random_choice]
print(person_who_will_pay + " Is going to pay the bill!")
我从 udemy 的 Angela Yu 那里花了 100 天的代码,我目前在随机模块 Day-4 中,但我不明白这段代码,谁能解释一下这段代码发生了什么。
答案 0 :(得分:0)
names_string = input("Give me everybody's names, separated by a comma. ")
# Gets input from the user as a string type.
names = names_string.split(", ")
# Splits the input string into a list of names.
# If names_string = "John, Mark, Mary", names = ["John", "Mark", "Mary"]
import random
num_items = len(names)
# Gets number of names in the list
random_choice = random.randint(0, num_items - 1)
# Selects a random index in the array from 0 to (number of names - 1)
person_who_will_pay = names[random_choice]
# Gets name stored at the chosen index.
print(person_who_will_pay + " Is going to pay the bill!")
# Prints person's name