Generate a random number between 1 and 3 to represent the computer's choice as follows: 1 -rock 2 -paper 3 -scissors
Generate a random number between 1 and 3 to represent the computer's choice as follows:
1 -rock
2 -paper
3 -scissors
The function displays the choice the computer has made and returns the random number.
The computer choice should be made randomly so that the game will be fair to the human player. Python provides a library of functions that generate random numbers. Libraries contain useful functions that are not built-in but instead, must be importedto use.The library module is called randomand the Python statement to import the library so the functions can be used is:
import random
The import statement is placed at the top of the file, before any functions are defined. The function to use is called randint(begin, end). The two parameters representthe beginning and ending numbers to use to generate a random number. The syntax to call a library function is to write the name of function followed by the dot operator (.) followed by the function name and parameters. For this problem, a random number between 1 and 3 can be generated by this statement:
choice = random.randint(1, 3)
The pseudocode
import random
computerSelection()
choice = random.randint(1,3)
If the integer = 1
display "Computer chose rock"
else if the integer = 2
display "Computer chose paper"
else
display "Computer chose scissors"
return choice
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images