Night 9: Roll the dice
이미 Python 또는 의사 코드로 이날의 학습을 마쳤습니다. 두 언어 모두에서 완료한 것으로 인정됩니다. 원하면 다시 연습하세요.
이 페이지는 최신 브라우저(SharedArrayBuffer 지원)가 필요합니다. Chrome, Edge, Firefox 또는 Safari를 최신 버전으로 업데이트해 주세요.
English
Tonight: roll the dice
- Games need surprises. Tonight your pet rolls a dice.
- You will learn one thing: random numbers.
Words for tonight
| Word | Meaning |
|---|---|
| random | you cannot know it before; it can be different each time |
| import | bring in extra tools for your program |
| choice | one thing picked from many |
English
Bring in the random tools
- Python has a box of tools called
random. Writeimport randomat the top. random.randint(1, 6)gives a whole number from 1 to 6. Both 1 and 6 can come.
import random
roll = random.randint(1, 6)
print("Your pet rolled", roll)
- Press Run a few times. The number changes!
English
Pick from a list
random.choicepicks one item from a list.
import random
toys = ["ball", "kite", "box"]
toy = random.choice(toys)
print("Let's play with the", toy)
English
How does the checker test random code?
- The checker cannot know your number. So it checks the rules:
- Is
rollfrom 1 to 6? Istotalreallya + b? - So use the variable names the task gives you.
English
Random plus a loop
- Put
randintinside awhileloop to roll again and again.
import random
roll = 0
while roll != 6:
roll = random.randint(1, 6)
print("Rolled", roll)
print("Six!")
- Now roll some dice in the tasks!
난이도 2/5 · 140코인 + 오늘 첫 완료 시 작은 보너스 추가
Your pet rolls a dice. import random, then put a random whole number from 1 부터 6 in roll. Print roll.
출력을 보려면 '실행'을 클릭하세요.
난이도 2/5 · 140코인 + 오늘 첫 완료 시 작은 보너스 추가
사용법 random.choice to pick one snack from the list. Put it in snack, then print Tonight: and the snack.
출력을 보려면 '실행'을 클릭하세요.
난이도 2/5 · 140코인 + 오늘 첫 완료 시 작은 보너스 추가
Roll two dice: a 및 b, each from 1 부터 6. Put their sum in total and print it.
출력을 보려면 '실행'을 클릭하세요.
난이도 3/5 · 200코인 + 오늘 첫 완료 시 작은 보너스 추가
Roll a dice again and again until you get a 6. Count the rolls in rolls. After the loop, print Six!.
출력을 보려면 '실행'을 클릭하세요.