You are on page 1of 3

Guess the number:

Type Example

built-in function print('You guessed',guess)

imported function random.randint()

literal str 'The number was’

literal int 1

module random

operator We don’t have any in this code.

keyword import

Q3 Look at the screenshot for Guess the Number. The results are currently printed as:

The number was 6.


You guessed 3.

Modify your Guess the Number code so that it prints the results like this:

The number was 6, but you guessed 3.

(You only need to include the part of the code that you modified.)\

Modified code: print('The number was',number,',but you guessed',guess,'.')

print('The number was ' + str(number) + ', but you guessed ' + guess + '.')

Design
1. When the game starts, the user is asked to guess
a number between 1 and 10.
2. After the user enter an answer, the user is
prompted to enter the enter key.
3. The computer gives the computer’s selected
number.
4. The computer display the number the user
guessed.
Function Test
1. Are the instructions displayed?
2. Is the player prompted to enter the number?
3. Does the program wait for the player to press
enter?
4. Does the program display the correct answer?
5. Does the program display users answer?
6. Does the game end?
7. Restart the game
 Is the generated number random?
Algorithms:
Main Algorithm:

Display instructions
Prompt player for guess
Display result
Display users guess

Algorithm blocks:

Display instructions
Line 1
Prompt player for guess
Line 2: guess = input('What is the number?')
Display result
Display computer’s selected number
Display users guess
Display the player’s guessed number

Code:

#A random number between 1 and 10 will be generate.


#The user will be asked to guess that number.

#Import function:
import random

#Coding:
def main():
print('I am thinking of a number between 1 and 10.')
number = random.randint(1,10)
guess = input('what is the number?')
print('The number was',number)
print('You guessed',guess)

main()

You might also like