livepython.net

99. Python Program to Make a Number Guessing Game

Hardwhile or for

Overview

Put the whole chapter together: an input loop with two ways out, and an if after the loop to find out which one happened.

Problem Statement

The secret number is 42. The player has up to 5 tries. Read one guess per line (a whole number):

  • if the guess is lower than 42, print Too low;
  • if it is higher, print Too high;
  • if it is right, stop asking and print Correct! Tries used: followed by the number of tries.

If all 5 guesses are wrong, print Out of tries! The number was 42 after the fifth one. For example, for 50, 25 and 42:

Too high
Too low
Correct! Tries used: 3

Don't read any more lines after a right guess. A real game would pick a random secret number; you'll learn the random module in chapter 18.

Use input() with no prompt text: the tests type the input for you, and a prompt would end up in your output.

Input

One guess per line: up to 5 lines, ending at the right guess. Example:

50
25
42

Output

The output for that example:

Too high
Too low
Correct! Tries used: 3

The tests try 4 different inputs, including a right guess on the fifth try.

Learn this firstChapter 11 · while Loop vs for Loop in Python: Which to Use?
solution.py
# write your code here

EXPECTED OUTPUT

1Too high
2Too low
3Correct! Tries used: 3

YOUR OUTPUT

Press Run to see your output here. Lines are compared with the expected output.