Night 4: Feed your pet
你已使用 Python 或伪代码完成过这个夜间练习,两种语言均计入完成记录。你可以再次练习。
今晚:喂养你的宠物
- 你的宠物饿了。它需要能量。
- 你将学习一件事:数字和数学运算。
今晚的词语
| 词 | 意义 |
|---|---|
| 数字 | 例如 3 或 2.5;不加引号 |
| 加 | 将数字相加:+ |
| 乘 | 进行乘法:*(不是 x) |
数字没有引号
5是一个数字。你可以用它进行数学运算。"5"是文本。它只是符号 5。
print(2 + 3)
print("2" + "3")
- 第一行显示
5。第二行显示23:它将文本连接起来。

数学符号
| 符号 | 含义 | 示例 | 答案 |
|---|---|---|---|
+ |
加 | 7 + 2 |
9 |
- |
减 | 7 - 2 |
5 |
* |
乘 | 7 * 2 |
14 |
/ |
除 | 7 / 2 |
3.5 |
改变盒子里的数字
- 要给变量增加数值,在右侧使用它的旧值。
- Python 先计算右侧,然后将结果填入盒子。
energy = 3
energy = energy + 2
print(energy)
将文本转换为数字
input()给出文本。int( )将文本转换为整数。
print("How many snacks?")
snacks = int(input())
print(snacks * 3)
- 如果没有
int,"2" * 3会产生222。试试看!
现在轮到你
- 完成下面的任务。点击运行,然后点击检查答案。
- 今晚好好喂养你的宠物。
难度 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. How many apples does it eat in 7 days? Put the answer in a variable apples_in_week. Use * and the variable apples. Then print apples_in_week. The output is 28.
点击“运行”查看此处输出。
难度 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.
点击“运行”查看此处输出。