Night 17: Spell it out
Bạn đã hoàn thành đêm này bằng Python hoặc mã giả. Kết quả được tính cho cả hai. Bạn có thể luyện tập lại nếu muốn.
Tonight: spell it out
- Your pet is learning to read. Tonight it looks at words letter by letter.
- You will learn one thing: a
forloop over text.
Words for tonight
| Word | Meaning |
|---|---|
| character | one letter, number, space or sign |
| letter | a, b, c … |
| backwards | from the end to the start |
Loop through text
- A
forloop can walk through a text, one character at a time.
word = "moon"
for letter in word:
print(letter)
- It works like a list:
"moon"is a row ofm,o,o,n.
Count some letters
- A loop, an
ifand a counter find how many of one letter there are.
word = "banana"
count = 0
for letter in word:
if letter == "n":
count = count + 1
print(count)
Build new text
- Start with empty text
"". Add to it in the loop.
| Code in the loop | "star" gives |
|---|---|
result = result + letter |
star (same order) |
result = letter + result |
rats (backwards) |

Is it one of these?
incan check if a character is in a text:
letter = "e"
if letter in "aeiou":
print(letter, "is a vowel")
- Now spell some words for your pet!
Độ khó 2/5 · 140 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Read a word. Use a for loop to print each letter on its own line. kit gives k, i, t.
Nhấn Chạy để xem kết quả ở đây.
Độ khó 2/5 · 140 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Read a word. Count how many times the letter a is in it, and print the count. banana gives 3.
Nhấn Chạy để xem kết quả ở đây.
Độ khó 3/5 · 200 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Read a word. Build it backwards in result, one letter at a time, and print it. star gives rats.
Nhấn Chạy để xem kết quả ở đây.
Độ khó 3/5 · 200 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Read a word. Print it with every vowel (a, e, i, o, u) changed to *. hello gives h*ll*.
Nhấn Chạy để xem kết quả ở đây.