결과보고 잘 풀었는줄 알았지만 사실 상 이 코드로 하면 안되는 것...
choice()를 쓰지 말고 풀라고 했는데ㅋㅋㅋㅋㅋ
제대로 안 읽고 냅다 풀고 빨리 풀었다고 좋아했음ㅋㅋㅋㅋ
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 1 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
NOTE: Don't worry about getting hold of the input(), we've done the work behind the scenes to import everything.
HINT: Assume that names looks like this: input: x, y, z, names = ["x", "y", "z"]
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!
결과적으로 choice() 를 쓰지 않고 문제를 푼다면
names = names_string.split(", ")
# The code above converts the input into an array seperating
#each name in the input by a comma and space.
# 🚨 Don't change the code above 👆
#파이썬 len()함수 사용해서 리스트에 있는 항목의 개수 알아내기
import random
names_len = len(names)
#print(names_len)
#리스트의 인덱스가 0부터 시작하기 때문에 -1을 해줘야함
#random.randint를 통해 0부터 마지막 인덱스 사이에 있는 임의의 숫자를 얻음
random_choice = random.randint(0,names_len - 1)
payer = names[random_choice]
# print(payer)
print(f"{payer} is going to buy the meal today!")
그리고 내가 choice()로 풀었던 방식
names = names_string.split(", ")
# The code above converts the input into an array seperating
#each name in the input by a comma and space.
# 🚨 Don't change the code above 👆
import random
meal_payer = random.choice(names)
print(f"{meal_payer} is going to buy the meal today!")
'python' 카테고리의 다른 글
[Python] ADDING EVEN NUMBERS (0) | 2023.11.02 |
---|---|
[python]파이썬 for문 반복문 (0) | 2023.11.01 |
[python]Heads or Tails 문제 풀기 (0) | 2023.10.27 |
[python] DAY 3 - LOVE CALCULATOR 풀기 (1) | 2023.10.26 |
[Python] 파이썬의 숫자처리 및 F-String (0) | 2023.10.24 |