Night 7: Count the stars
このページには最新のブラウザ(SharedArrayBuffer対応)が必要です。Chrome、Edge、Firefox、Safariを最新バージョンに更新してください。
English
Tonight: count the stars
- It is your pet's first week! Tonight it counts the stars.
- You will learn one thing: the
forloop.
Words for tonight
| Word | Meaning |
|---|---|
| loop | do the same thing again and again |
| repeat | do again |
| range | a row of numbers, like 1, 2, 3, 4, 5 |
English
Repeat without typing again
- Five
printlines is a lot of typing. A loop does it for you. - The indented lines run again for each number.
for n in range(1, 6):
print("Star number", n)
English
How range counts
| You write | You get |
|---|---|
range(5) |
0 1 2 3 4 |
range(1, 6) |
1 2 3 4 5 |
range(2, 11, 2) |
2 4 6 8 10 |
rangestops before the last number.
English
Loop through a list
- A
forloop can also walk through a list, one item at a time.
snacks = ["apple", "rice", "fish"]
for snack in snacks:
print("Yum,", snack)
English
Inside or after the loop?
- Indented lines are inside the loop. They repeat.
- Lines with no indent come after the loop. They run once.
for i in range(3):
print("Zzz")
print("Good night!")
English
Week 1 is done!
- You know
print, variables,input(), numbers,if, lists and loops. - That is the base of almost every program.
- Finish the tasks below and your pet grows up a stage.
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
Your pet counts stars. Use a for loop with range to print the numbers 1 から 5, one per line.
実行ボタンをクリックして出力を確認してください。
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
Use a for loop to print I like and each snack in the list. The first line is I like apple.
実行ボタンをクリックして出力を確認してください。
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
Read a number with int(input()). Print Zzz that many times, one per line. Then print Good night! once, at the end.
実行ボタンをクリックして出力を確認してください。
難易度 3/5 · 200 コイン + 今日初回達成時の小ボーナス
Your pet saw stars on 4 nights: stars = [3, 5, 2, 6]. Use a loop to add them all up in a variable total. Then print total. The output is 16.
実行ボタンをクリックして出力を確認してください。