Write a program that tests your ESP extrasensory perception
Write a program that tests your ESP (extrasensory perception). The program should randomly select the name of a color from the following list of words:
Red, Green, Orange, Yellow
To select a word, the program can generate a random number. For example, if the number is 0, the selected word is Red; if the number is 1, the selected word is Green; and so forth.
Next, the program should ask the user to enter the color that the computer has selected. After the user has entered his or her guess, the program should display the name of the randomly selected color. The program should repeat this 10 times and then display the number of times the user correctly guessed the selected color.
Solution
from random import randint #importing randint from random library to generate random number
colors = [\"Red\", \"Green\", \"Orange\", \"Yellow\"] #colors provided
correct_guesses = 0 #initialising the number of correct guesses
for i in range(10):
rand_no = randint(0, 3) # random integer between 0 to 3
color = colors[rand_no] #selecting the numbered color from the list of colors
color_input = raw_input(\"Input a color as a guess: \") #taking guess as input
if color_input.lower() == color.lower(): #checking if guess is right
print \"The color was:\", color
print \"You are right!\"
correct_guesses += 1 #incrementing the number of correct guesses
else:
print \"The color was:\", color
print \"You are wrong!\"
print \"The number of times you guessed correctly:\", correct_guesses #printing out the correct guesses
