python code informactice 3 question

profilejenny2000000
AlgorithmExample.docx

Algorithm Example

For the following task:

Use the random module to write a number guessing game.

The number the computer chooses should change each time you run the program.

Repeatedly ask the user for a number. If the number is different from the computer's let the user know if they guessed too high or too low. If the number matches the computer's, the user wins.

Keep track of the number of tries it takes the user to guess it.

An appropriate algorithm might be:

Import the random module

Display a welcome message to the user

Choose a random number between 1 and 100

Get a guess from the user

Set a number of tries to 0

As long as their guess isn’t the number

Check if guess is lower than computer

If so, print a lower message.

Otherwise, is it higher?

If so, print a higher message.

Get another guess

Increment the tries

Repeat

When they guess the computer's number, display the number and their tries count

Notice that each line in the algorithm corresponds to roughly a line of code in Python, but there is no coding itself in the algorithm. Rather the algorithm lays out what needs to happen step by step to achieve the program.