개인적인 공부/Python
day 4-4 python (roulette, 룰렛)
karatejin
2023. 2. 24. 13:25
Instructions
You are going to write a program that will select a random name from a list of names. The person selected will have to pay for everybody's food bill.
Important: You are not allowed to use the choice() function.
Line 8 splits the string names_string into individual names and puts them inside a List called names. For this to work, you must enter all the names as names followed by comma then space. e.g. name, name, name
Example Input
Angela, Ben, Jenny, Michael, Chloe
Note: notice that there is a space between the comma and the next name.
Example Output
Michael is going to buy the meal today!
들어가기전 split()
# Split string method
names_string = input("Give me everybody's names, separated by a comma. ")
names = names_string.split(", ") ##-- 문자열을 쪼개준다
# Import the random module here
import random
# 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 👇
num_items = len(names) ##-- 각 이름 목록의 길이
random_choice = random.randint(0, num_items - 1) ##-- 처음 부터 끝까지. 길이가 정해져 있지 않기 때문에 배열의 마지막 -1 을 부른다.
who_pay_all = names[random_choice]
print(who_pay_all + "is going to buy the meal today!")
한번에 하는 법도 있다.
who_pay_all = random.choice(names)
#한번에 하는 법도 있다