#import random library
import random
#colors list array
colors = ["Red", "Blue", "Green", "Brown","Pink", "White", "Black", "Silver"]
#Print Welcome Message
def welcome_msg():
print("Welcome to the guessing game! ")
#Generate the random index for the colors array list and return the index
def get_random_color_index():
randomIndex = random.choice(colors)
print(randomIndex)
#Get user guess input and return user input
def get_user_guess():
userGuess = input("Pick a Color:" )
while(True):
if userGuess == colors:
print(userGuess)
else:
print("Please pick a differnt color: ")
#check if user input and random index color matches
def check_guess_color_match(index, user_guess_color):
pass
##############Guess Game program#####################
#print welcome massage first
welcome_msg()
#Ask user to enter their guessed colors
get_user_guess()
#Get the random guess color based on the random index generator.
get_random_color_index()
#check if the user guess color matches the computer random color
#based on the random index
#print if user guess correct or fail
user input can accept all case option ( i.e. user can enter "Red", "red", or "RED") and extra space if entered by mistake( " Red" , "Red " , and " red ") and user input of color must be from list. if not then ask the user to re-enter.
CodePudding user response:
If you have any questions about how to code then write to me on discord. RobertK#6151
Solution:
import random
# Colors list
colors = ['red', 'blue', 'green', 'brown', 'pink', 'white', 'black', 'silver']
# Print Welcome Message
print('Welcome to the guessing game!')
# Generate the random color
random_color = random.choice(colors)
correct_guess_format = False
while not correct_guess_format:
# Get user guess input
user_guess = input('Pick a color: ').replace(' ', '').lower()
# Check if user input is not in colors list
if user_guess not in colors:
print('Color not in color list, pick again!')
else:
correct_guess_format = True
# Check if player won
if user_guess == random_color:
print('Correct! You won.')
else:
print('Wrong! You failed.')
CodePudding user response:
Resubmitted due to wrong formatting. In this version I made the get_user_guess function more like your original version. Feel free to ask any clearing questions.
import random
# Colors list
colors = ['red', 'blue', 'green', 'brown', 'pink', 'white', 'black', 'silver']
# Print Welcome Message
def welcome_msg():
print('Welcome to the guessing game!')
# Generate the random color
def get_random_color():
return random.choice(colors)
# Get and return user input
def get_user_guess():
user_guess = input('Pick a color: ').replace(' ', '').lower()
while True:
if user_guess in colors:
return user_guess
else:
print('Please pick a different color!')
# Check if user input and random color matches
def check_guess_color_match(random_color, user_guess_color):
return random_color == user_guess_color
'''########## Guess Game Program ##########'''
# Print welcome message first
welcome_msg()
# Ask user to enter their guessed colors, returns a color
user_picked_color = get_user_guess()
# Get the random color, returns a color
random_picked_color = get_random_color()
# Check if the user guess color matches the computer random color, returns a boolean (True or False)
do_colors_match = check_guess_color_match(random_picked_color, user_picked_color)
# Print if user guess is correct or false
if do_colors_match:
print('Correct! You won.')
else:
print('Wrong! You failed.')