Night 10: Guess my number
你已使用 Python 或伪代码完成过这个夜间练习,两种语言均计入完成记录。你可以再次练习。
Tonight: guess my number
- Tonight you build your first game!
- Your pet thinks of a number. The player guesses. Your pet says "too low" or "too high".
- Nothing new to learn: you use
INPUT,IF,WHILEand a counter together.
Words for tonight
| Word | Meaning |
|---|---|
| guess | an answer when you are not sure |
| secret | something only one person knows |
| hint | a small help that shows the way |
Step 1: one guess
- Start small. Check one guess and give one hint.
- 9618 has no
ELSEIF. Put a secondIFinside theELSE.
DECLARE Secret, Guess : INTEGER
Secret ← 7
Guess ← 5
IF Guess < Secret THEN
OUTPUT "Too low"
ELSE
IF Guess > Secret THEN
OUTPUT "Too high"
ELSE
OUTPUT "You got it!"
ENDIF
ENDIF
- Change
Guessto 9, then 7, and press Run each time.
Step 2: guess again and again
- Put the hint inside a
WHILEloop. The loop goes on while the guess is wrong.
| Line | What it does |
|---|---|
WHILE Guess <> Secret |
go on while the guess is wrong |
IF … ELSE … ENDIF |
give a hint |
INPUT Guess |
read the next guess |
- After
ENDWHILE, the guess must be right. OutputYou got it!.
Step 3: count the tries
- A counter tells the player how well they did.
Tries ← 1at the start, thenTries ← Tries + 1after each new guess.
A real game uses random
- In a real game, the secret is different each time:
DECLARE Secret : INTEGER
Secret ← INT(RAND(10)) + 1
OUTPUT "I am thinking of a number from 1 to 10."
- In the tasks the secret is always
7. Then the checker can test your game fairly. - When you finish, change
7toINT(RAND(10)) + 1and play it yourself!
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
Your pet's secret number is 7. Read one guess with INPUT Guess. Output Too low, Too high 或 You got it!.
点击“运行”查看此处输出。
难度 3/5 · 今日首次完成可获 200 金币及少量额外奖励
Now let the player guess again and again. After each wrong guess, output Too low 或 Too high and read the next guess. When the guess is right, output You got it!.
点击“运行”查看此处输出。
难度 3/5 · 今日首次完成可获 200 金币及少量额外奖励
Count the guesses in Tries. At the end, output You got it in , the number of tries, then tries!. For guesses 3, 9, 7 the last line is You got it in 3 tries!.
点击“运行”查看此处输出。
难度 4/5 · 今日首次完成可获 280 金币及少量额外奖励
Make the game harder: only 3 tries. If the player gets it, output You got it!. If all 3 guesses are wrong, output Too low 或 Too high for each, then Out of tries! It was 7.
点击“运行”查看此处输出。