Night 4: Feed your pet
Tonight: feed your pet
- Your pet is hungry. It needs energy.
- You will learn one thing: numbers and maths.
Words for tonight
| Word | Meaning |
|---|---|
| number | like 3 or 2.5; no quotes |
| add | put numbers together: + |
| times | multiply: * (not x) |
Numbers have no quotes
5is a number. You can do maths with it."5"is text. It is just the symbol 5.
print(2 + 3)
print("2" + "3")
- The first line shows
5. The second line shows23: it joins the text.
Maths signs
| Sign | Meaning | Example | Answer |
|---|---|---|---|
+ |
add | 7 + 2 |
9 |
- |
take away | 7 - 2 |
5 |
* |
times | 7 * 2 |
14 |
/ |
divide | 7 / 2 |
3.5 |
Change a number in a box
- To add to a variable, use its old value on the right.
- Python works out the right side first. Then it fills the box.
energy = 3
energy = energy + 2
print(energy)
Text into a number
input()gives text.int( )turns text into a whole number.
print("How many snacks?")
snacks = int(input())
print(snacks * 3)
- Without
int,"2" * 3makes222. Try it!
Now you try
- Do the tasks below. Press Run, then Check answer.
- Feed your pet well tonight.
Сложность 1/5 · 100 монет + небольшой бонус за первое завершение сегодня
Your pet has energy = 3. A snack gives it 2 more. Add 2 to energy, then print energy. The output is 5.
Нажмите Запустить, чтобы увидеть результат здесь.
Сложность 2/5 · 140 монет + небольшой бонус за первое завершение сегодня
Your pet eats apples = 4 apples each day. Make a variable week that holds how many apples it eats in 7 days. Use *. Then print week.
Нажмите Запустить, чтобы увидеть результат здесь.
Сложность 2/5 · 140 монет + небольшой бонус за первое завершение сегодня
The user types how many snacks to give. Read it with int(input()). Your pet starts with energy = 10. Each snack adds 3. Print the new energy. For 2 snacks, the output is 16.
Нажмите Запустить, чтобы увидеть результат здесь.
Сложность 3/5 · 200 монет + небольшой бонус за первое завершение сегодня
You have cookies = 17 cookies and pets = 5 pets. Make each = cookies each pet gets (a whole number, use //). Make left = cookies left over (use %). Print each, then left.
Нажмите Запустить, чтобы увидеть результат здесь.